LeetCode 21. Merge Two Sorted Lists合并两个有序链表 (C++)
题目:
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.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
分析:
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
创建一个新的节点head,比较了l1和l2的值的大小,将较小的加入到head,依次比较,如果l1,l2有剩余,就接在后面即可。
l1 l2 head,p
↓ ↓ ↓
1->2->4 1->3->4 0
l1 l2 head p
↓ ↓ ↓ ↓
2->4 1->3->4 0->1
l1 l2 head p
↓ ↓ ↓ ↓
2->4 3->4 0->1->1
l1 l2 head p
↓ ↓ ↓ ↓
4 3->4 0->1->1->2
l1 l2 head p
↓ ↓ ↓ ↓
4 4 0->1->1->2->3
l1 l2 head p
↓ ↓ ↓ ↓
null 4 0->1->1->2->3->4
l1 l2 head p
↓ ↓ ↓ ↓
null null 0->1->1->2->3->4->4
最后返回head.next即可。
还可以递归求解此问题。
mergeTwoLists( l1, l2) //l1:1->2->4,l2:1->3->4
=>{1} + mergeTwoLists( l1, l2) //l1:2->4,l2:1->3->4
=>{1->1} + mergeTwoLists( l1, l2) //l1:2->4,l2:3->4
=>{1->1->2} + mergeTwoLists( l1, l2) //l1:4,l2:3->4
=>{1->1->2->3} + mergeTwoLists( l1, l2) //l1:4,l2:4
=>{1->1->2->3->4} + mergeTwoLists( l1, l2) //l1,l2:4
=>{1->1->2->3->4->4}
程序:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode head();
ListNode* p = &head;
while(l1 && l2){
if(l1->val < l2->val){
p->next = l1;
l1 = l1->next;
}
else{
p->next = l2;
l2 = l2->next;
}
p = p->next;
}
if(l1) p->next = l1;
if(l2) p->next = l2;
return head.next;
}
};
//递归
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1 == nullptr) return l2;
if(l2 == nullptr) return l1;
if(l1->val < l2->val){
l1->next = mergeTwoLists(l1->next, l2);
return l1;
}
else{
l2->next = mergeTwoLists(l1, l2->next);
return l2;
}
}
};
LeetCode 21. Merge Two Sorted Lists合并两个有序链表 (C++)的更多相关文章
- leetcode 21 Merge Two Sorted Lists 合并两个有序链表
描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...
- [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 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)
这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...
- 21. Merge Two Sorted Lists(合并2个有序链表)
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...
- [LeetCode] 23. Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...
- [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 ...
- 021 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 ...
随机推荐
- CF-1155 D.Beautiful Array
题目大意:现在有一个数列,还有一个数字x,你可以将这个数列中的一段连续子序列同时乘以这个数字x(当然也可以不乘),然后问你最大子段和是多少 做法:dp,你懂的 #include<iostream ...
- [LOJ 2083][UOJ 219][BZOJ 4650][NOI 2016]优秀的拆分
[LOJ 2083][UOJ 219][BZOJ 4650][NOI 2016]优秀的拆分 题意 给定一个字符串 \(S\), 求有多少种将 \(S\) 的子串拆分为形如 AABB 的拆分方案 \(| ...
- Paper | Spatially Adaptive Computation Time for Residual Networks
目录 摘要 故事 SACT机制 ACT机制 SACT机制 实验 发表在2017年CVPR. 摘要 在图像检测任务中,对于图像不同的区域,我们可以分配不同层数的网络予以处理. 本文就提出了一个基于Res ...
- Oracle中如何创建数据库
Oracle数据库的物理结构与MySQL以及SQLServer有着很大的不同.在使用MySQL或SQLServer时,我们不需要去关心它们的逻辑结构和物理结构. 但是在使用Oracle的时候,我们必须 ...
- iOS Workflow 分享 - Scan QR Code
很多时候我们无意识地用微信扫描一个 QR Code,然后无论打开的是什么我们用就是了.我经常会好奇到底 QR Code 编码的是什么信息,到底是一个 ID 呢,还是一个 URL(可能是 deeplin ...
- Program 3 – CS 344
Program 3 – CS 344OverviewIn this assignment you will write your own shell in C, similar to bash. No ...
- learning rate warmup实现
def noam_scheme(global_step, num_warmup_steps, num_train_steps, init_lr, warmup=True): ""& ...
- python 统计使用技巧
python 统计使用技巧 # 1.不输入回车获取值 注:需要tty模块配合. fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) ...
- .net post请求webservice
class Program { static void Main(string[] args) { , name = "jxp" }); var a = HttpHelper.Po ...
- DevExpress的TreeList怎样设置数据源,从实例入手
场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...