当前位置:网站首页>【LeetCode】3. Merge Two Sorted Lists·合并两个有序链表
【LeetCode】3. Merge Two Sorted Lists·合并两个有序链表
2022-07-03 07:34:00 【AQin1012】
题目描述
英文版描述
You are given the heads of two sorted linked lists list1 and list2. Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list.
英文版地址
中文版描述
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例 1:
输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]
示例 2:
输入:l1 = [], l2 = []
输出:[]
示例 3:
输入:l1 = [], l2 = [0]
输出:[0]
Constraints·提示:
两个链表的节点数目范围是 [0, 50]
-100 <= Node.val <= 100
l1 和 l2 均按 非递减顺序 排列
中文版地址
解题思路
我的第一反应就是全部读取,然后一个一个比较,再放进一个新的链表里,后来又想了想,这两个输入的链表是已经排好序的,那么其实可以从分别从两个链表读取数据做比较,小的那个存进新的链表并指向该链表的下一个数据,大的继续跟这个新数据做比较……如此重复,直到有一个链表空了,把未空的链表剩余的数据加在新链表尾部,返回新链表即可。
解题方法
俺这版
递归法
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if (list1 == null) {
return list2;
}
else if (list2 == null) {
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.
public class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
官方版
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode prehead = new ListNode(-1);
ListNode prev = prehead;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
prev.next = l1;
l1 = l1.next;
} else {
prev.next = l2;
l2 = l2.next;
}
prev = prev.next;
}
// 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可
prev.next = l1 == null ? l2 : l1;
return prehead.next;
}
// Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
欢迎有更好方法的同学同学评论区踢我(。・ω・。)ノ
边栏推荐
- Logging log configuration of vertx
- Vertx's responsive MySQL template
- 技术干货|昇思MindSpore初级课程上线:从基本概念到实操,1小时上手!
- IO stream system and FileReader, filewriter
- Hello world of vertx
- The difference between typescript let and VaR
- Common architectures of IO streams
- Jeecg menu path display problem
- 4everland: the Web3 Developer Center on IPFs has deployed more than 30000 dapps!
- Technical dry goods | reproduce iccv2021 best paper swing transformer with Shengsi mindspire
猜你喜欢
Technical dry goods Shengsi mindspire operator parallel + heterogeneous parallel, enabling 32 card training 242 billion parameter model
Custom generic structure
Take you through the whole process and comprehensively understand the software accidents that belong to testing
項目經驗分享:實現一個昇思MindSpore 圖層 IR 融合優化 pass
技术干货|百行代码写BERT,昇思MindSpore能力大赏
Map interface and method
【MySQL 14】使用DBeaver工具远程备份及恢复MySQL数据库(Linux 环境)
《指環王:力量之戒》新劇照 力量之戒鑄造者亮相
技术干货|昇思MindSpore NLP模型迁移之LUKE模型——阅读理解任务
技术干货|昇思MindSpore NLP模型迁移之Bert模型—文本匹配任务(二):训练和评估
随机推荐
Le Seigneur des anneaux: l'anneau du pouvoir
2. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
技术干货|昇思MindSpore NLP模型迁移之LUKE模型——阅读理解任务
Chrome 98 Private Network Access problem w/ disabled web security: Request had no target IP address
技术干货|关于AI Architecture未来的一些思考
Various postures of CS without online line
Lombok -- simplify code
带你全流程,全方位的了解属于测试的软件事故
Image recognition and detection -- Notes
Leetcode 213: looting II
1. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
Industrial resilience
为什么说数据服务化是下一代数据中台的方向?
Technical dry goods Shengsi mindspire elementary course online: from basic concepts to practical operation, 1 hour to start!
I. D3.js hello world
技术干货|AI框架动静态图统一的思考
New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears
Paper learning -- Study on the similarity of water level time series of Xingzi station in Poyang Lake
gstreamer ffmpeg avdec解码数据流向分析
FileInputStream and fileoutputstream