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 ...
随机推荐
- python3 pyqt5 和eric5配置教程
一.大纲内容: 1.预备PC环境: 2.预备安装程序: 2.1.下载Python3.2 2.2.下载PyQt4 2.3.下载Eric5 3.安装配置步骤: 3.1.安装Pyhon3.2 3.2.安装P ...
- Microsoft Expression Blend 4制作简单的按钮
在博客园混了这么久了,第一次写博客.本人标准的理工男,文笔不敢说一般,只能用还学过语文.勉强达意而已.见笑!! 由于本人能有有限,错误之处在所难免,望大牛们批评指正,共同进步.^_^!!!!!!!!! ...
- opencv学习笔记(03)——遍历图像(迭代器法)
#include <opencv2\highgui\highgui.hpp> #include <opencv2\imgproc\imgproc.hpp> #include & ...
- javascript看你能够做对几题
http://ourjs.com/detail/52fb82e13bd19c4814000001
- PD 脚本中列名注释用Name属性
操作步骤:Database=>Generate Datatabase=>Format选项卡=>勾选 Generate name in empty comment项
- 结构体 typedef关键字
1 结构体 #include <iostream> #include <cstring> using namespace std; void printBook( struct ...
- spoj 297
就是对距离进行二分找最大值 .... #include <cstring> #include <cstdio> #include <algorithm> #incl ...
- 重新学struct,边界对齐,声明……与Union的区别
在内存中,编译器按照成员列表顺序分别为每个结构体变量成员分配内存,当存储过程中需要满足边界对齐的要求时,编译器会在成员之间留下额外的内存空间. 如果想确认结构体占多少存储空间,则使用关键字sizeof ...
- UrlRewriteFilter 美化器的使用方法 伪静态化的解决方案(转)
一,URL美化器简介 UrlRewriteFilter是一个用于改写URL的Web过滤器,类似于Apache的mod_rewrite.适用于任何Web应用服务器(如Resin,Orion,Tomcat ...
- json中jobject
Json.net codeplex :http://www.codeplex.com/Json 原本感觉Newtonsoft.Json和.net自己的JavaScriptSerializer相差无几, ...