[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.
问题:将两个已排序的列表,合并为一个有序列表。
令 head 为两个列表表头中较小的一个,令 p 为新的已排序的最后一个元素。令 l1, l2 分别为两个列表中未排序部分的首节点。依次将 l1, l2 中的较小值追加到 p 后面,并调整 p 和 l1、l2较小者指针即可。
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (l1 == NULL) {
return l2;
} if (l2 == NULL) {
return l1;
} ListNode* head = new ListNode();
if (l1->val <= l2->val) {
head = l1;
l1 = l1->next;
}else{
head = l2;
l2 = l2->next;
} ListNode* p = head; while(l1 != NULL && l2 != NULL){ if(l1->val <= l2->val){
p->next = l1;
p = p->next;
l1 = l1->next;
}else{
p->next = l2;
p = p->next;
l2 = l2->next;
}
} if (l1 == NULL) {
p->next = l2;
} if (l2 == NULL) {
p->next = l1;
} return head;
}
[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 ...
- 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 ...
- [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 ,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
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)
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
随机推荐
- python django model类型摘要
V=models.CharField(max_length=None[, **options]) #varchar V=models.EmailField([max_length=75, **opti ...
- WIN32读写INI文件方法
在程序中经常要用到设置或者其他少量数据的存盘,以便程序在下一次执行的时候可以使用,比如说保存本次程序执行时窗口的位置.大小.一些用户设置的 数据等等,在 Dos 下编程的时候,我们一般自己产生一个 ...
- [转] 强大的python字符串解析
1.python字符串通常有单引号('...').双引号("...").三引号("""...""")或('''...'' ...
- 多线程、Service与IntentService的比较
资料摘自网络(侵删) Service Thread IntentService AsyncTask When to use ? Task with no UI, but shouldn't b ...
- 关于lower_bound()的用法--NYOJ 201作业题
lower_bound它有三个参数, 第一个和第二个是给定区间起点和终点的指针,第三个参数是要查找的数,它的作用原理是在给定的区间中进行二分查找,这个二分区间是前开后闭的,他返回第一个大于等于它的函数 ...
- java的@see注释
@see注释用法 @see 类名 @see #方法名或属性名 @see 类名#方法名或属性名
- oracle存储过程调试方法
PL/SQL中为我们提供了[调试存储过程]的功能,可以帮助你完成存储过程的预编译与测试. 点击要调试的存储过程,右键选择TEST 如果需要查看变量,当然调试都需要.在右键菜单中选择Add debug ...
- 单点登录CAS使用记(二):部署CAS服务器以及客户端
CAS-Server下载地址:https://www.apereo.org/projects/cas/download-cas CAS-Client下载地址:http://developer.jasi ...
- java(try块语句变量,和匿名类变量生存时间
在try块定义的变量不能作用于快外 // int a=2; try{ int a=3; System.out.println(a); } catch(Exception e){} System.out ...
- [转]extern,static存储空间矛盾
其实,这两个语句的位置不同,会出现不同的解释.这主要是由于 static 具有的两重意义所导致的: (1) 如果 static int foo; 这一句位于函数中,则 static 表示的是存储属性, ...