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 splicing together the nodes of the first two lists.
解题思路:
题目的意思是将两个有序链表合成一个有序链表。
逐个比较加入到新的链表即可。
代码如下:
public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode list = new ListNode(0);
ListNode tmp = list;
while (l1 != null || l2 != null) {
if (l1 == null) {
tmp.next = new ListNode(l2.val);
l2 = l2.next;
} else if (l2 == null) {
tmp.next = new ListNode(l1.val);
l1 = l1.next;
} else {
if (l1.val < l2.val) {
tmp.next = new ListNode(l1.val);
l1 = l1.next;
} else {
tmp.next = new ListNode(l2.val);
l2 = l2.next;
}
}
tmp = tmp.next;
}
return list.next;
}
Java [leetcode 21]Merge Two Sorted Lists的更多相关文章
- [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 ,java
题目: 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 splicing t ...
- # 蜗牛慢慢爬 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 splicin ...
- 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 23]Merge k Sorted Lists
题目描述: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complex ...
- [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 ...
随机推荐
- 在Linux中,如何取出一个字符串的前5位
问: 在Linux中,如何取出一个字符串的前5位? 常用的一些方法如下: [tough@toughhou ~]$ str=abcdef [tough@toughhou ~]$ echo $str ab ...
- lua通过bunlde读注意事项
把lua脚本做成bundle,加载字符串,变成lua对象: lua loadstring("name")() 注意:loadstring的问题: 无法访问全局local变量,需要改 ...
- (转)Const,Const函数,Const变量,函数后面的Const
本文转自http://www.cnblogs.com/Fancyboy2004/archive/2008/12/23/1360810.html 看到const 关键字,C++程序员首先想到的可能是co ...
- C#中 Thread.Sleep精度问题
Thread.Sleep的精度默认在15ms左右,如果需要类似 Thread.Sleep(1)的精细控制,需要调用特定的平台API实现 [DllImport("winmm.dll" ...
- hdu 4712
看了大牛的解法 第一次知道可以产生随机数解题 在计算hamming距离时用了位运算 很简便 /************************************************* ...
- 从List[Future[T]]到Future[List[T]]
在课程<Principles Of Reactive Programming>里Week3的一节 "Promises, promises, promises"中,Eri ...
- jmeter summariser(命令行执行时的输出) 、查看结果树等结果中文乱码
在使用jmeter测试时,如果你的sampler名字为中文.或者输出的结果信息有中文,你会发现它们都是乱码,非常蛋碎!原因是: jmeter的默认编码为:ISO-8859-1, 解决方案就是要修改它 ...
- ajax的GET和POST请求
GET和POST请求 GET请求时最常见的请求类型,用于向服务器查询信息,必要时可以将查询字符串参数放在URL尾部发送给服务器,如果参数有特殊字符必须正确编码.我们上面使用的例子都是使用GET请求,非 ...
- PHP基础语法3
文件系统 判断文件是否存在 如果只是判断文件存在,使用file_exists就行,file_exists不仅可以判断文件是否存在,同时也可以判断目录是否存在,从函数名可以看出, is_file是确切的 ...
- 【mysql的设计与优化专题(4)】表的垂直拆分和水平拆分
垂直拆分 垂直拆分是指数据表列的拆分,把一张列比较多的表拆分为多张表 通常我们按以下原则进行垂直拆分: 把不常用的字段单独放在一张表; 把text,blob等大字段拆分出来放在附表中; 经常组合查询的 ...