# 蜗牛慢慢爬 LeetCode 21. Merge Two Sorted Lists [Difficulty: Easy]
题目
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.
翻译
合并两个有序的链表
Hints
Related Topics: LinkedList
参考 归并排序-维基百科,可以递归也可以迭代,基本的链表操作
代码
Java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1==null) return l2;
if(l2==null) return l1;
if(l1.val<l2.val){
l1.next = mergeTwoLists(l1.next,l2);
return l1;
}else{
l2.next = mergeTwoLists(l1,l2.next);
return l2;
}
}
}
Python
//recursively
class Solution(object):
def mergeTwoLists(self, l1, l2):
if l1==None: return l2;
if l2==None: return l1;
if l1.val<l2.val:
l1.next = self.mergeTwoLists(l1.next,l2)
return l1
else:
l2.next = self.mergeTwoLists(l1,l2.next)
return l2
//iteratively
class Solution(object):
def mergeTwoLists(self, l1, l2):
dummy = curr = ListNode(0)
while l1 and l2:
if l1.val<l2.val:
curr.next = l1
l1 = l1.next
else:
curr.next = l2
l2 = l2.next
curr = curr.next
curr.next = l1 or l2
return dummy.next
# 蜗牛慢慢爬 LeetCode 21. Merge Two Sorted Lists [Difficulty: Easy]的更多相关文章
- 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]
题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- Leetcode 21. Merge Two Sorted Lists(easy)
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- 21. Merge Two Sorted Lists【easy】
21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...
- [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 splicing t ...
- [leetcode] 21. Merge Two Sorted Lists (Easy)
合并链表 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class ...
- 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 splicing t ...
- Java [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 ...
随机推荐
- solr的随机排序 【转载】
原文地址:http://blog.csdn.net/duck_genuine/article/details/8477336 有这样的一种需求,想从索引库里随机取出4条记录. 在 schema.xml ...
- pycharm 取消空格,逗号 等符号的自动补全
- testNG参数化
听说testNG比junit更好用,记录下 环境:springboot2.0+testNG6.8+maven+myeclipse 一 安装 (1)m'yeclipse安装testNg包,下载testN ...
- HDU1599(Floyd最小环)
Floyd最小环理解+模板: https://www.cnblogs.com/DF-yimeng/p/8858184.html 除了上述博文里写的,我再补充几点我的理解. 1.为什么先枚举ij求经过i ...
- flask 蓝本(blueprint)
蓝本(blueprint) 一.基本概念: 将视图方法模块化,既当大量的视图函数放在一个文件中,很明显是不合适,最好的方案是根据功能将路由合理的划分到不同的文件中:而蓝本就是为了解决这个问题而出现的. ...
- uC/OS-III 时钟节拍,时间管理,时间片调度
uC/OS-III 时钟节拍,时间管理,时间片调度 时钟节拍 时钟节拍可谓是 uC/OS 操作系统的心脏,它若不跳动,整个系统都将会瘫痪. 时钟节拍就是操作系统的时基,操作系统要实现时间上的管理, ...
- EXCEL2007出错了无法使用文档中的ActiveX 控件
EXCEL2007出错了无法使用文档中的ActiveX 控件虽说是很久之前的问题,但是正确的解决方法和原因如下!原因:系统update之后出现的问题解决方法:删除C:\Users\[username] ...
- Xcode添加全局引用文件pch
Xcode6之前有PrefixHeader.pch文件在写项目的时候,大部分宏定义.头文件都导入在这个pch文件,虽然方便,但会增加Build的时间,所以Xcode6以及之后的版本去除了PrefixH ...
- odoo之ERP系统
odoo大纲 第一部分:数据库postgressql 大象 第二部分:ORM(API) 第三部分:客户端 用python软件写: .py文件 包含两部分:1.自定义部分,由自己写,定义类和功能. .继 ...
- Iframe和Frame中实现cookie跨域的方法(转载)
在Iframe和Frame中默认是不支持Cookie跨域的,但通过设置P3P协议相关的响应头可以解决这一问题.关于p3p协议: P3P: Platform for Privacy Preference ...