leetcode 【 Linked List Swap Nodes in Pairs 】 python 实现
题目:
Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
代码:oj测试164ms通过
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param a ListNode
# @return a ListNode
def swapPairs(self, head): if head is None or head.next is None:
return head dummyhead = ListNode(0)
dummyhead.next = head
p = dummyhead while p.next is not None and p.next.next is not None:
tmp = p.next.next.next
p.next.next.next = p.next
p.next = p.next.next
p.next.next.next = tmp
p = p.next.next return dummyhead.next
思路:
1. 建立一个虚表头hummyhead 这样方便操作一些
2. p.next始终指向要交换的下一个元素的位置
3. 先保存p.next.next.next,再移动p,p.next,p.next.next,p.next.next.next:先动p.next.next.next再动其他的。
小白我一开始先动的是p,p.next结果后面的p.next.next就丢了,其他小白别陷入这个误区,高手请略过。
Tips: 动了哪个指针,就把哪个指针上面打个×;添加了哪个指针,就在两个点之间加一根线;画画图就出来了,别光看着不动笔。
又做了一遍 第二次ac的 小失误了
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param a ListNode
# @return a ListNode
def swapPairs(self, head):
if head is None or head.next is None:
return head dummpyhead = ListNode(0)
dummpyhead.next = head p = dummpyhead while p.next is not None and p.next.next is not None:
tmp = p.next
p.next = p.next.next
tmp.next = p.next.next
p.next.next = tmp
p = p.next.next return dummpyhead.next
leetcode 【 Linked List Swap Nodes in Pairs 】 python 实现的更多相关文章
- 【LeetCode练习题】Swap Nodes in Pairs
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- Leetcode 线性表 Swap Nodes in Pairs
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...
- 【LeetCode】24. Swap Nodes in Pairs (3 solutions)
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- LeetCode解题报告—— Swap Nodes in Pairs & Divide Two Integers & Next Permutation
1. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For e ...
- 【一天一道LeetCode】#24. Swap Nodes in Pairs
一天一道LeetCode系列 (一)题目 Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- [LeetCode 题解]:Swap Nodes in Pairs
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a li ...
- leetcode 题解 || Swap Nodes in Pairs 问题
problem: Given a linked list, swap every two adjacent nodes and return its head. For example, Given ...
- [Linked List]Swap Nodes in Pairs
Total Accepted: 73777 Total Submissions: 219963 Difficulty: Medium Given a linked list, swap every t ...
- 【LeetCode】24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
随机推荐
- java——二叉树面试题
import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util ...
- Prim算法求权数和,POJ(1258)
题目链接:http://poj.org/problem?id=1258 解题报告: #include <iostream> #include <stdio.h> #includ ...
- 基于Mybatis的Dao层开发
转自:https://www.cnblogs.com/rodge-run/p/6528398.html 基于Mybatis的Dao层开发 SqlSessionFactoryBuilder用于创建 Sq ...
- 2017.11.13 在C语言中是否能用函数实现模块化程序设计
第七章 用函数实现模块化程序设计 (1)为什么要用函数? @function既是函数也是功能.函数就是用来完成一定功能的的(函数就是功能),函数名就是给这个功能起一个名字,一个C程序可由一个主函数和若 ...
- sql插入临时表数据的方法
方法有两种,主要看需求. 方法1:定义好临时表的字段和类型.插入对应的值 create table #Tmp --创建临时表#Tmp ( City varchar(), -- Country varc ...
- Spring注解@Value数值取值转换字符串失败
配置文件(yml)中,配置项如下: cebconfig: INST_CODE: 08801001 SFT_NOTIFY_CEB_CHANNEL: 123456 期望INST_CODE: 0880100 ...
- 通过ip地址访问同一局域网下已经启动的angular项目
通常tomcat启动的项目同一局域网下我们都可以访问.angular启动的前台项目别人怎么访问,一直不懂,后来知道启动命令加个参数就行了 首先查看本机ip 第二步,启动命令里加上--host 本机ip ...
- Docker自学纪实(六)搭建docker私有仓库
docker的镜像仓库分两种:一种是从官方公有仓库拉取:还有就是自己搭建私有仓库.官方的镜像仓库是面对整个应用市场的:私有仓库一般用于公司内部,就是公司项目自身所需的镜像.搭建私有仓库有什么好处?私有 ...
- LeetCode978. 最长湍流子数组
问题:978. 最长湍流子数组 当 A 的子数组 A[i], A[i+1], ..., A[j] 满足下列条件时,我们称其为湍流子数组: 若 i <= k < j,当 k 为奇数时, A[ ...
- linux面试集
shell:1.$# 和 $*之类的特殊变量 特殊变量列表 变量 含义 $0 当前脚本的文件名 $n 传递给脚本或函数的参数.n是一个数字,表示第几个参数.例如,第一个参数就是$1 $# 传递给脚本或 ...