# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Lists
https://oj.leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked lists and return it as a new list.
The new list should be made by splicing together the nodes of the first two lists. ===Comments by Dabay===
基本链表的操作。先做一个头节点,用两个指针来记录两个链表的位置。
比较两个链表的节点,把小的挂后边,之后移动指针。
最后,当一个链表已经比较完了之后,把另外一个链表中剩下的部分挂上。
''' # Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None class Solution:
# @param two ListNodes
# @return a ListNode
def mergeTwoLists(self, l1, l2):
node = root = ListNode(0)
node1 = l1
node2 = l2
while node1 and node2:
if node1.val < node2.val:
node.next = node1
node1 = node1.next
else:
node.next = node2
node2 = node2.next
node = node.next
if node1:
node.next = node1
else:
node.next = node2
return root.next def main():
sol = Solution()
l1 = ListNode(1)
l2 = ListNode(2)
merged = sol.mergeTwoLists(l1, l2)
node = merged
while node:
print "%s ->" % node.val,
print "End" if __name__ == '__main__':
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[Leetcode][Python]21: Merge Two Sorted Lists的更多相关文章

  1. 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...

  2. [Leetcode][Python]23: Merge k Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...

  3. C# 写 LeetCode easy #21 Merge Two Sorted Lists

    21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...

  4. Leetcode练习题21. Merge Two Sorted Lists

    题目描述(easy) Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new ...

  5. 【一天一道LeetCode】#21. Merge Two Sorted Lists

    一天一道LeetCode系列 (一)题目 Merge two sorted linked lists and return it as a new list. The new list should ...

  6. LeetCode 【21. Merge Two Sorted Lists】

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  7. 【LeetCode】21. Merge Two Sorted Lists

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  8. 【leetcode】 21. Merge Two Sorted Lists

    题目描述: Merge two sorted linked lists and return it as a new list. The new list should be made by spli ...

  9. LeetCode:21. Merge Two Sorted Lists(Easy)

    1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...

随机推荐

  1. PHP扫雷(转载)。

    <?php   @$init = $_POST["init"];//game restart  @$clickvalue = $_POST["clickvalue& ...

  2. distance.c

    #include "stdio.h" #include "string.h" #include "math.h" #include &quo ...

  3. python2.X和python3.X在同一平台下的切换技巧

    python2.X和python3.X在同一平台下的切换技巧 最近在自己的电脑上同时安装了python2.7.11和python3.5.1 在网上搜了一些答案,主要还是参照<learning p ...

  4. 新手讲树:证明任意二叉树度为零的节点n0,永远比度为2的节点n2多1个

    证明:   设度为1的节点个数为n1,因为二叉树的所有节点的度都小于等于2, 所以n=n0+n1+n2; 又因为二叉树中,除了根节点所有的节点都有一个进入节点的分支,假设B为所有的分支,那么n=B+1 ...

  5. Intellij IDEA开发第一个android应用教程

    用惯eclipse的同学们可以试试通过Intellij IDEA来开发一个android应用.下面是具体的教程. 首先:下载Intellij IDEA.最新版本是12.官方提供两个版本.一个是Comm ...

  6. static wechat red package tool

    ---------------------------------------------------------------------------------------------------- ...

  7. Hibernate、批量操作数据

    Hibernate 批量操作数据可以使用两种方法实现 1.分批更新,每一小批同步一次数据: public void saveEmployee2(){ Session s=HibernateSessio ...

  8. C#学习基础总结

    概念:.net与c#.net/dontnet:一般指.net framework框架,一种平台,一种技术c#(charp):一种编程语言,可以开发基于.net的应用. *java既是一种技术又是一种编 ...

  9. asp.net 超链接 下载TEXT文件,而不是直接在IE中打开

    问题描述:后台生成了文本文件,用超链接提供给用户下载.点击超链接,下载Excel文件没问题,但文本文件会直接被打开,而不是弹出下载窗口. 解决方法:把HyperLink改为LinkButton,在Cl ...

  10. JAVA读取propertise配置文件

    java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"key=value"的格式,在pr ...