# -*- 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. android:versionCode和android:versionName

    Android的版本可以在androidmainfest.xml中定义,主要有android:versionCode和android:versionName android:versionCode:主 ...

  2. Oracle EBS-SQL (PO-6):检查订单接收总数.sql

    SELECT sum(rcvt.quantity) 接收事务处理汇总数--已排除退货 --rsh.receipt_num                   收据号, --pov.vendor_nam ...

  3. Delphi中的消息截获(六种方法:Hook,SubClass,Override WndProc,Message Handler,RTTI,Form1.WindowProc:=@myfun)good

    Windows是一个基于消息驱动的系统,因此,在很多时候,我们需要截获一些消息然后自己进行处理.而VCL系统又有一些特定的消息.下面对我所了解的delphi环境中截获消息进行一些总结.      就个 ...

  4. Memcached简明介绍

    官网介绍:http://memcached.org/ Free & open source, high-performance, distributed memory object cachi ...

  5. MD5 32位、16位加密

    /// <summary> /// MD5 16位加密 /// </summary> /// <param name="ConvertString"& ...

  6. Python学习笔记8-类的继承 、深度优先、广度优先

    Python 类声明 语法: class 类名: 类体 例: #--encoding:utf-8-- # class AddressBookEntity: myVersion=0.1 def __in ...

  7. Redis简介以及如何在Windows上安装Redis

    Redis简介 Redis是一个速度非常快的非关系型内存数据库. Redis提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erla ...

  8. oracle查看系统资源占用情况

    1,连上服务器,使用top命令,可以查看cpu使用率以及内存的使用情况等等,还有当前各用户的使用情况 2,用pl/sql developper,tool里面选sessions,就可以看到当前sessi ...

  9. iOS判断并使用百度地图 高德地图 导航 (使用URI,不集成sdk)

    [objc] view plaincopy  1. BOOL hasBaiduMap = NO;   2.         BOOL hasGaodeMap = NO;   3.            ...

  10. js实现睡眠

    //js暂停函数 function Pause(obj, iMinSecond) { if (window.eventList == null) window.eventList = new Arra ...