# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairs
https://oj.leetcode.com/problems/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. ===Comments by Dabay===
链表的操作。主要就是赋值next的时候,如果这个next本来指向的node还有用,就把它先记录下来。
这里用previous指向要交换的一对node的前面一个node。要交换的node依次为node1和node2。
''' # 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):
previous = new_head = ListNode(0)
new_head.next = head
while previous and previous.next and previous.next.next:
node1 = previous.next
node2 = node1.next
node1.next = node2.next
previous.next = node2
node2.next = node1
previous = node1
return new_head.next def print_listnode(node):
while node:
print "%s->" % node.val,
node = node.next
print "END" def main():
sol = Solution()
node = root = ListNode(1)
for i in xrange(2, 5):
node.next = ListNode(i)
node = node.next
print_listnode(root)
print_listnode(sol.swapPairs(root)) if __name__ == '__main__':
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[Leetcode][Python]24: Swap Nodes in Pairs的更多相关文章

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

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

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

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

  4. LeetCode OJ 24. Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  5. LeetCode:24. Swap Nodes in Pairs(Medium)

    1. 原题链接 https://leetcode.com/problems/swap-nodes-in-pairs/description/ 2. 题目要求 给定一个链表,交换相邻的两个结点.已经交换 ...

  6. 24. Swap Nodes in Pairs

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

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

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

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

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

随机推荐

  1. 在PHP代码中处理JSON 格式的字符串的两种方法:

    总结: 在PHP代码中处理JSON 格式的字符串的两种方法: 方法一: $json= '[{"id":"1","name":"\u ...

  2. 转发:使用sql命令查询视图中所有引用的基础表

    转自:使用sql命令查询视图中所有引用的基础表 使用sql命令查询视图中所有引用的基础表 之前有写过如何利用sql查询视图中所有引用的表发现这个方法并不能查出视图中所有的基础表,如果视图中有嵌套视图就 ...

  3. PostgreSql入门命令

    1 命令行登录数据库 有两种方式,一是直接在系统shell下执行psql命令:而是先进入psql环境,然后再连接数据库.下面分别给出实例: (1)直接登录 执行命令:psql -h 192.168.1 ...

  4. delphi 读网页线程TReadHtmlThread

    读网页,通常是一个耗时操作.故把读网页放入线程是显得比较重要了. 本例用改进后的 TIdhttpEx 加上线程来实现读网页. 它的父类TSimpleThread 在此 本例程源码在此 源码中包含了所有 ...

  5. C语言的本质(4)——浮点数的本质与运算

    C语言的本质(4)--浮点数的本质与运算 C语言规定了3种浮点数,float型.double型和long double型,其中float型占4个字节,double型占8个字节,longdouble型长 ...

  6. 基于本地iso 搭建的本地yum源 安装部署openldap

    1,yum openldap-servers,openldap-clients 基于iso-cd1搭建的本地yum源(具体搭建参看ruige的repo本地快速搭建,在右边 找找看中输入repo key ...

  7. GCDAsyncUdpSocket的使用

    tips: 要注意服务器端口与客户端端口的区别,如果客户端绑定的是服务器的端口,那么服务器发送的消息就会一直发送给服务器.

  8. Binary String Matching(kmp+str)

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  9. Js 自定义回调函数

    参考 http://mlxnle.iteye.com/blog/1670679 <!doctype html> <html lang="es"> <h ...

  10. JS 精粹( 函数)

    函数是对象,它与其它对象唯一的不同是它可以调用.函数可实现:代码复用.信息隐藏.代码组合调用. 建立函数时会建立:上下文.调用函数的代码.每个函数(除Function.prototype)都会有一个原 ...