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 ...
随机推荐
- python 字符串部分总结
字符串 对于单个字符的编码,Python提供了ord()函数获取字符的整数表示,chr()函数把编码转换为对应的字符 >>> ord('A') 65 >>> ord ...
- 【BZOJ1269】[AHOI2006] 文本编辑器editor(Splay)
点此看题面 大致题意: 让你维护一个字符串,有插入字符串.删除区间.反转区间和输出单个字符操作. \(Splay\) 这应该是一道比较简单的\(Splay\)题(虽然因为各种细节我调了很久). 我们可 ...
- 过河问题(POJ1700)
题目链接:http://poj.org/problem?id=1700 解题报告: 1.贪心算法,每次过两个速度最慢的人,抵消那个较慢的人的时间. #include <stdio.h> # ...
- python 面向对象(一)--类(class)和实例(Instance)
面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各自的数据可 ...
- CUDA memory
原文链接 CUDA存储器类型: 每个线程拥有自己的register and loacal memory; 每个线程块拥有一块shared memory; 所有线程都可以访问global memory; ...
- 第48章 MDK的编译过程及文件类型全解—零死角玩转STM32-F429系列
第48章 MDK的编译过程及文件类型全解 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.co ...
- C# 声明bool变量
与现实世界不同,在编程的世界中,每一件事情要么黑,要么白:要么对,要么错:要么是真的,要么是假的.例如,假定你创建一个名为x的整数变量,把值99赋给x,然后问:“x中包含了值99吗?”答案显然是肯定的 ...
- orale 10g和11g中的自动统计任务
orale 10g和11g中的自动统计任务 博客分类: 数据库相关/oracle 1) 先来看下oracle 10g中的自动统计任务的问题. 从Oracle Database 10g开始,Or ...
- 解题:在下面画线的地方填任何代码,使得最终输出 'hello world',至少写五个不同思路的方案
今天(已经好些天前了...),群里面(JS前端开发跳板6群[81501322])有个群友问了这样一个问题. 如题:在下面画线的地方填任何代码,使得最终输出 'hello world',至少写五个不同思 ...
- Codeforces#498F. Xor-Paths(折半搜索)
time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standa ...