[Leetcode][Python]24: Swap Nodes in Pairs
# -*- 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的更多相关文章
- 【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 ...
- 【一天一道LeetCode】#24. Swap Nodes in Pairs
一天一道LeetCode系列 (一)题目 Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- 【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 ...
- 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 ...
- LeetCode:24. Swap Nodes in Pairs(Medium)
1. 原题链接 https://leetcode.com/problems/swap-nodes-in-pairs/description/ 2. 题目要求 给定一个链表,交换相邻的两个结点.已经交换 ...
- 24. Swap Nodes in Pairs
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- 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 ...
- 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 ...
- [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)
Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...
随机推荐
- 论山寨手机与Android 【11】移动网络规范的合纵连横
上一章我们讨论了SmartPhone BP部分的硬件系统,接下去我们将讨论SmartPhone BP部分的软件系统.所谓BP,指的是基带处理器(Baseband Processor),又称为通讯处理器 ...
- Delphi Window 消息大全使用详解
WM_CTLCOLORSTATIC = $0138; 当一个静态控件将要被绘制时发送此消息给它的父窗口:通过响应这条消息,所有者窗口可以通过使用给定的相关显示设备的句柄来设置静态控件的文本和背景颜色 ...
- C#句柄使用
原文:C#句柄使用 调用 API 函数 SendMessage 发送 WM_CLOSE 消息. DllImport("User32.dll",EntryPoint="Se ...
- Android stagefright与opencore对比
[转载至其它博客] http://blog.csdn.net/djy1992/article/details/9339917 1引言 Android froyo版本多媒体引擎做了变动,新添加了st ...
- aix archPlat
#++++++++++++++++++++++++++++++++++++++++++++++++++++ #+ Ruiy(R) Techdiss contact: 150 5519 8367 #+ ...
- iOS中UITextView键盘回收
iOS开发中,发现UITextView没有像UITextField中textFieldShouldReturn:这样的方法,那么要实现UITextView关闭键盘,就必须使用其他的方法,下面是可以使用 ...
- Android4.2.2的Stagefright维护编解码器的数据流
这里是他们自己的源代码阅读点滴总结属性,转请注明出处,谢谢. 欢迎和大家分享.qq:1037701636 email:gzzaigcn2012@gmail.com Android源代码版本号Versi ...
- [UI]抽屉菜单DrawerLayout分析(二)
继续分析DrawerLayout的手势分发部分 谈到手势分发,这本身就是个好话题,DrawerLayout作为继承自ViewGroup得布局他可以拦截手势也可以分发给子view,也就是在 onInte ...
- HDU1506_Largest Rectangle in a Histogram
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- JavaScript可以这样用
javascript:Qrlink(<%#Eval("ActivityType")%>,<%#Eval("ID")%>,<%#Ev ...