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 ...
随机推荐
- 10.python3实用编程技巧进阶(五)
5.1.如何派生内置不可变类型并修其改实例化行为 修改实例化行为 # 5.1.如何派生内置不可变类型并修其改实例化行为 #继承内置tuple, 并实现__new__,在其中修改实例化行为 class ...
- Note | Ubuntu18.04安装与配置
目标: 在服务器上配置最新的Ubuntu稳定版本18.04 LTS.18.04比16.04好看很多,非常建议. 有3块硬盘:2块4TB机械硬盘,1块2TB固态硬盘.计划将固态硬盘作为主硬盘,其余两块机 ...
- vscode配置python环境-运行调试-windows环境
官方文件介绍 https://code.visualstudio.com/docs/languages/python 准备: vscode下载安装 python3版本下载(安装时可以选择添加环境变量, ...
- 【转】pywinauto教程
一.环境安装 1.命令行安装方法 pip install pywinauto==0.6.7 2.手动安装方法 安装包下载链接:pyWin32: python调用windows api的库https:/ ...
- CentOS7系统yum方式安装MySQL5.7
参考:https://www.cnblogs.com/bigbrotherer/p/7241845.html#top 1.在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要, ...
- 2019-9-2-win10-uwp-判断本地ip
原文:2019-9-2-win10-uwp-判断本地ip title author date CreateTime categories win10 uwp 判断本地ip lindexi 2019-0 ...
- Hbase基本原理
一.hbase是什么 HBase 是一种类似于数据库的存储层,也就是说 HBase 适用于结构化的存储.并且 HBase 是一种列式的分布式数据库,是由当年的 Google 公布的 BigTable ...
- Java性能 -- CAS乐观锁
synchronized / Lock / CAS synchronized和Lock实现的同步锁机制,都属于悲观锁,而CAS属于乐观锁 悲观锁在高并发的场景下,激烈的锁竞争会造成线程阻塞,而大量阻塞 ...
- flux架构的详细介绍和使用!
结构分为四个 视图 view动作 action派发器 dispatcher数据商店 store 流程: 用户操作视图 视图(view)发送动作(action)到派发器(dispatcher) 由派发器 ...
- php中函数 isset(), empty(), is_null() 的区别,boolean类型和string类型的false判断
php中函数 isset(), empty(), is_null() 的区别,boolean类型和string类型的false判断 实际需求:把sphinx返回的结果放到ssdb缓存里,要考虑到sph ...