[Leetcode][Python]21: Merge Two Sorted Lists
# -*- 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的更多相关文章
- 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- [Leetcode][Python]23: Merge k Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...
- 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 ...
- 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 ...
- 【一天一道LeetCode】#21. Merge Two Sorted Lists
一天一道LeetCode系列 (一)题目 Merge two sorted linked lists and return it as a new list. The new list should ...
- 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 ...
- 【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 ...
- 【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 ...
- LeetCode:21. Merge Two Sorted Lists(Easy)
1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...
随机推荐
- poj中一些对我来说不错的东西(每天不同的加入,要保持)
1.关于深度搜索与暴力结合的棋盘翻转问题 poj1753::2965:: 2.贪心算法:2109,2586: 3.韩信点兵问题:poj1006
- yum 安装软件时报Public key for * is not installed
这个是由于没有导入rpm签名信息引起的 解决方案: rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
- SQL Server 数据库状态选项
选项 1. single_user(单用户),multi_user(多用户),restricted_user(受限用户); 描述数据库的用户访问属性,它们互斥,设置其中任何一个选项就会取消对其它选项的 ...
- 论山寨手机与Android联姻 【6】MTK手机的基带芯片
MTK的硬件技术的核心,在于它的基带芯片.为了降低成本,同时缩减手机主板的面积,基带芯片中除了CPU以外,还集成了很多外设控制器.Feature Phone的功能,基本上取决于基带芯片所支持的外设功能 ...
- Delphi资源文件(全面分析之位图、光标、图标、AVI、JPEG、Wave)
几乎每个Windows应用程序都使用图标.图片.光标等资源.资源是程序的一部分,但是它是不可执行代码.下面我们就详细介绍资 源文件在Delphi5中建立和使用方法. 1.把资源放到Exe文件的优点 ...
- 在strings.xml中定义html标签
在项目的开发过程中,需要用到把html内容放到strings.xml文件中,然后再读取到TextView中.原本以为像普通文本一样直接SetText就行了,结果行不通,大大超出我的预料.经过网上搜索, ...
- document.body.scrollTop与document.documentElement.scrollTop兼容
这两天在写一个JS的网页右键菜单,在实现菜单定位的时候发现了这个问题:chrome居然不认识document.documentElement.scrollTop! 看前辈们的文章,纷纷表示如果有文档声 ...
- 自己主动下载源代码_并编译_打包_部署_重新启动服务的Shell脚本
这里面Shell的各个操作含义,可參考我三年前的这篇文章:http://blog.csdn.net/jadyer/article/details/7960802 #!/bin/sh APP_NAME= ...
- JavaScript 工作必知(九)function 说起 闭包问题
大纲 Function Caller 返回函数调用者 Callee 调用自身 作用域 闭包 function 函数格式 function getPrototyNames(o,/*optional*/ ...
- MSDN地址,记录下来,以防以后使用
MSDN在线官网:https://msdn.microsoft.com/zh-cn/default.aspx 以备学习时候使用.