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.

找时间加入递归的写法

class Solution(object):
def swapPairs(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head==None or head.next==None:
return head
temp_head=ListNode(-1)
temp_head.next=head
prehead=temp_head
while prehead.next!=None and prehead.next.next!=None:
a=prehead.next
b=a.next
prehead.next,a.next,b.next=b,b.next,a
prehead=a
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. java布局学习 (二)

    前文中介绍了FlowLayout和BorderLayout 本文我们将会继续介绍java中的布局方式 (3)GridLayout 网格布局 这种布局会将整个容器划分成M行*N列的网格. 如下图:    ...

  2. win7,安装node失败

    win7下,下载node安装包,安装之后 打开命令行输入 node -v,仍然提示命令不可用. 解决办法: 找到环境变量: 在用户变量里修改 path: 添加系统变量 NODE_PATH: 关机重启电 ...

  3. [kohana] kohana3.2,如何兼容PDO数据库连接方式

    由于历史原因,有个kohana3.2的站点需要搬迁到php5.5上来,但php5.5已经不支持mysql_connect()这个函数了,只能使用PDO来连接数据库. 但换上PDO之后,报了这个的一个错 ...

  4. 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数017·point点函数

    <zw版·Halcon-delphi系列原创教程> Halcon分类函数017·point点函数 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号“**”,替换:“p ...

  5. 提供一个表单,进行post数据处理

    var strContent = "aaaaa"; html.Append("<html><body><form id='postUploa ...

  6. GitHub上整理的一些工具

    技术站点 Hacker News:非常棒的针对编程的链接聚合网站 Programming reddit:同上 MSDN:微软相关的官方技术集中地,主要是文档类 infoq:企业级应用,关注软件开发领域 ...

  7. Selenium 2.0 + Java 入门之环境搭建

    最近在研究Java+Selenium的自动化测试,网上的资料比较多,自己测试实践后,整理出来一套相对比较完善的环境资料,因为网上很多下载实践的过程中,发现出现了很多不匹配的问题,什么jdk和eclip ...

  8. jquery autocomplete

    <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="http: ...

  9. EF连接ORACLE

    1.nuget引用Oracle.ManagedDataAccess.EntityFramework的dll文件 2.安装Oracle Developer Tools for Visual Studio ...

  10. 。net 之view筛选

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...