[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 ...
随机推荐
- 如果有需要确解MD5的,可以尝试这个网站。
http://www.hashkiller.co.uk/md5-decrypter.aspx
- Handle 消息机制
android中Handle类的主要作用: 1.在新启动的线程中发送给消息 2.在主线程获取.处理消息 为什么要用Handle这样的一个机制: 因为在Android系统中UI操作并不是线程安全的,如果 ...
- php实现多表(四表)连接
<?php include_once "DBHelper.php"; define('HOST', '127.0.0.1'); define('USER', 'root'); ...
- 分支-03. 三天打鱼两天晒网-B3
/*B3-分支-03. 三天打鱼两天晒网 *Main.c *测试通过 */ #include <stdio.h> #include <stdlib.h> int main() ...
- ajax动态加入的元素不被jquerymobile渲染问题
一:对于listview: $(‘ul’).listview(‘refresh’); 二:对于div或其他: $(‘allAddContent’).trigger( “create” );
- 又是玻璃效果?调用一句代码就OK了
原文 http://www.cnblogs.com/lan-mei/archive/2012/05/11/2495740.html 最近自学WPF,网上一查资料,全是依赖属性,路由事件,动画效果等等. ...
- 又优化了一下 Android ListView 异步加载图片
写这篇文章并不是教大家怎么样用listview异步加载图片,因为这样的文章在网上已经有很多了,比如这位仁兄写的就很好: http://www.iteye.com/topic/685986 我也是因为看 ...
- Linux进程间通信——使用信号量
这篇文章将讲述别一种进程间通信的机制——信号量.注意请不要把它与之前所说的信号混淆起来,信号与信号量是不同的两种事物.有关信号的更多内容,可以阅读我的另一篇文章:Linux进程间通信——使用信号.下面 ...
- 用ATL和MFC来创建ActiveX控件
摘要:目前MFC和ATL代表了两种框架,分别面向不同类型的基于Windows的开发.MFC代表了创建独立的Windows应用的一种简单.一致的方法:ATL提供了一种框架来实现创建COM客户机和服务器所 ...
- Nginx 介绍和安装
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev ...