题目:

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 实现的更多相关文章

  1. 【LeetCode练习题】Swap Nodes in Pairs

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  2. Leetcode 线性表 Swap Nodes in Pairs

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...

  3. 【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 ...

  4. 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 ...

  5. 【一天一道LeetCode】#24. Swap Nodes in Pairs

    一天一道LeetCode系列 (一)题目 Given a linked list, swap every two adjacent nodes and return its head. For exa ...

  6. [LeetCode 题解]:Swap Nodes in Pairs

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a li ...

  7. leetcode 题解 || Swap Nodes in Pairs 问题

    problem: Given a linked list, swap every two adjacent nodes and return its head. For example, Given ...

  8. [Linked List]Swap Nodes in Pairs

    Total Accepted: 73777 Total Submissions: 219963 Difficulty: Medium Given a linked list, swap every t ...

  9. 【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 ...

随机推荐

  1. 模仿ArcGIS用Graphics重绘的直方图分级调节器

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  2. IOS 自定义代理delegate方法

    创建一个自定义代理 @class MJTgFooterView; /** 1.协议名称: 控件类名 + Delegate 2.代理方法普遍都是@optional 3. */ @protocol MJT ...

  3. php一个类引用另一个类的方法的写法

    default.php: <?php namespace SiteInfo{ class Site{ var $url; var $title; function setUrl($par){ $ ...

  4. LCT入门

    前言 \(LCT\),真的是一个无比神奇的数据结构. 它可以动态维护链信息.连通性.边权.子树信息等各种神奇的东西. 而且,它其实并不难理解. 就算理解不了,它简短的代码也很好背. \(LCT\)与实 ...

  5. Uva 12169 不爽的裁判 模运算

    题目链接:https://vjudge.net/contest/156903#problem/B 题意: 有一个递推公式 : a,b都不是已知的,给出了 x1,x3,x5.... 求x2,x4,x6. ...

  6. 课程设计__C++初步,C++对C的扩充

    小草的C++要结课了,小草终于翻起书来,小草用的老谭的书,有什么不对的就找老谭去吧. ///C++初步 ///C++对C的扩展 #include <iostream> using name ...

  7. 2018.7.31 oracle rownum的理解

    一.Rownum的描述: rownum是一个伪列,数据库中并不保存rownum的列值,它是oracle系统为返回的结果集顺序分配的行编号,rownum是随着结果集生成的,一旦生成,在同一个结果集中就不 ...

  8. HttpServerUtility 和 HttpUyility

    参考:msdn HttpServerUtility 提供用于处理 Web 请求的 Helper 方法. 2017/08/07            加密解码 这个类没有构造函数,所以不能直接new. ...

  9. SpringBoot学习5:访问静态资源

    springboot默认从项目的resources里面的static目录下或者webapp目录下访问静态资源 方式一:在resources下新建static文件(文件名必须是static) 在浏览器中 ...

  10. PureLayout,使用纯代码写AutoLayout

    为iOS和OS X的自动布局最终的API -- 令人印象深刻的简单,非常强大. PureLayout延伸的UIView /NSView , NSArray,和NSLayoutConstraint与之后 ...