当前位置:网站首页>LeetCode 21、合并两个有序链表
LeetCode 21、合并两个有序链表
2022-04-21 15:31:00 【亡于灬】
21、合并两个有序链表
1)题目描述
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例 1:

输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]
示例 2:
输入:l1 = [], l2 = []
输出:[]
示例 3:
输入:l1 = [], l2 = [0]
输出:[0]
提示:
- 两个链表的节点数目范围是
[0, 50] -100 <= Node.val <= 100l1和l2均按 非递减顺序 排列
2)分析
递归: 两个链表头部结点值较小的一个与剩下元素merge操作的结果合并,直到其中一个链表为空,递归结束。时间复杂度O(m+n),空间复杂度O(m+n)。
迭代: 递归的迭代写法。设定两个新的指针,preHead和preNext,分别用来指向头结点和要合并的结点。同时遍历两个链表,将他们头结点较小合并到preHead的尾部,直到其中一个链表为空,再将另一个非空链表剩下的部分合并到preHead尾部。
3)C++代码
递归
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */
class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
if(!list1)
return list2;
else if(!list2)
return list1;
else if(list1->val<list2->val){
list1->next=mergeTwoLists(list1->next,list2);
return list1;
}
else{
list2->next=mergeTwoLists(list1,list2->next);
return list2;
}
}
};
迭代
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */
class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode* preHead=new ListNode();
ListNode* preNext=preHead;
while(list1&&list2){
if(list1->val<list2->val){
preNext->next=list1;
list1=list1->next;
}
else{
preNext->next=list2;
list2=list2->next;
}
preNext=preNext->next;
}
preNext->next=!list1?list2:list1;
return preHead->next;
}
};
版权声明
本文为[亡于灬]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_38342510/article/details/124316020
边栏推荐
- Smart public security QR code positioning alarm system development of mobile police app
- 111页精细化工股份公司数据字转型解决方案
- mysql找不到my.ini文件
- With Zhongfu Jinshi's stock speculation, the industry has precipitated for 25 years, and the investment should speak with strength
- Deltix Round, Summer 2021 E. Equilibrium
- 产业园区数字孪生规划方案
- [Unity] error CS0433: The type ‘Task‘ exists in both Unity.Tasks,....
- 49页企业数字化转型案例及常用工具企业数字化转型思路
- AcWing 1812. Square pasture (enumeration)
- [binary search - medium] 1855 Maximum distance of subscript alignment (need to review double pointer method)
猜你喜欢

万有导航:简洁实用的综合导航网站

AcWing1800. Do not do the last (enumeration)

What is an email address? Easy to use email registration application

Jetpack compose uses custom operators to achieve the effect of drawing five pointed stars

Solve i.mx6u driver transplantation LED flashing

Web.xml文件详解

LeetCode-232-用栈实现队列
![[Unity] error CS0433: The type ‘Task‘ exists in both Unity.Tasks,....](/img/17/3a0b2e52336bc702ff3b38420e9e6a.png)
[Unity] error CS0433: The type ‘Task‘ exists in both Unity.Tasks,....

外贸公司一般用什么邮箱,电子邮件如何群发?

Introduction to openlayers (II)
随机推荐
AcWing 1788. Why do cows cross the road (simulation)
Ultimate doll 2.0 | observable practice sharing of cloud native PAAS platform
Mysql的安装与卸载
mysql 将某个字段修改成自增
[binary search - simple] LCP 28 Procurement scheme
Best practices | under the epidemic, learn how eolink can help telecommuting!
Design and implementation of public welfare website based on JSP
49页企业数字化转型案例及常用工具企业数字化转型思路
返璞归真,多方安全计算要回归到“安全”的本源考虑
SQL服务器如何设置起始日期查询语句
105 page digital twin city information model CIM platform construction technical scheme
商家该如何建立私域流量?
智慧园区数融通-数字化赋能运营管理平台解决方案
登录重构小记
Leetcode problem 652 finding duplicate subtrees
【常见问题】anaconda prompt报错solving environment:failed
【时序】Reformer:局部敏感哈希(LSH)实现高效 Transformer 论文笔记
读书破万“卷”:国民阅读洞察2022
How to apply for corporate email? How to register email with mobile phone number?
How should businesses establish private domain traffic?