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

找时间加入递归的写法

  1. class Solution(object):
  2. def swapPairs(self, head):
  3. """
  4. :type head: ListNode
  5. :rtype: ListNode
  6. """
  7. if head==None or head.next==None:
  8. return head
  9. temp_head=ListNode(-1)
  10. temp_head.next=head
  11. prehead=temp_head
  12. while prehead.next!=None and prehead.next.next!=None:
  13. a=prehead.next
  14. b=a.next
  15. prehead.next,a.next,b.next=b,b.next,a
  16. prehead=a
  17. return temp_head.next

24. Swap Nodes in Pairs的更多相关文章

  1. [Leetcode][Python]24: Swap Nodes in Pairs

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...

  2. 24. Swap Nodes in Pairs(M);25. Reverse Nodes in k-Group(H)

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

  3. leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法

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

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

  5. [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)

    Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...

  6. [LeetCode] 24. Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  7. Java [leetcode 24]Swap Nodes in Pairs

    题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-& ...

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

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

随机推荐

  1. 今天学了递归,感觉好复杂啊/(ㄒoㄒ)/~~

    honio塔思路: 第一步 把A上的n-1个圆盘借助C移到B上: 第二步 把A上的一个圆盘移到C上: 第三步 把B上的n-1个圆盘借助A移到C上. 这显然符合递归的两个条件: ①具备边界条件:只有1个 ...

  2. 5.开发webservice

    package com.atguigu.ws; import javax.jws.WebMethod; import javax.jws.WebService; /** * * @author Adm ...

  3. PHPExcel--基本操作

    下面是PHPExcel的导入与导出的基本操作,也是最重要的两个操作. 生成文件: <?php require_once './Classes/PHPExcel.php'; $content = ...

  4. UILabel内容模糊

    在非retina的ipad mini的屏幕上,一个UIlabel的frame的origin值如果有小数位数(例如0.5),就会造成显示模糊,所以最好使用整数的值作为origin坐标.

  5. Time crumbles things; everything grows old under the power of Time and is forgotten through the lapse of Time

    Time crumbles things; everything grows old under the power of Time and is forgotten through the laps ...

  6. DP专题训练之HDU 1506 Largest Rectangle in a Histogram

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  7. C# ASP.NET 读取EXCEL 单元格 读取 空值 不显示

    跟大家分享一下,[摘自]:http://blog.csdn.net/li185416672/article/details/8213729 读取excel时,某些单元格为空值 原来如此: 当我们用ol ...

  8. java.sql.SQLException: 索引中丢失 IN 或 OUT 参数:: x

    我的x值是2 我的SQL语句采用的是预编译的形式,我先单独把SQL语句提出来在数据库里运行正常,值也传输正常.仔细查了一下预编译的代码,发现当时粘贴复制 忘把序号修改了,改正后

  9. Candies-POJ3159差分约束

    Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse was the ...

  10. [poj2182] Lost Cows (线段树)

    线段树 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacula ...