当前位置:网站首页>【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;
}
}

欢迎有更好方法的同学同学评论区踢我(。・ω・。)ノ
边栏推荐
- [mindspire paper presentation] summary of training skills in AAAI long tail problem
- Some basic operations of reflection
- Chrome 98 Private Network Access problem w/ disabled web security: Request had no target IP address
- Jeecg data button permission settings
- 技术干货|昇思MindSpore初级课程上线:从基本概念到实操,1小时上手!
- Dora (discover offer request recognition) process of obtaining IP address
- 【MySQL 14】使用DBeaver工具远程备份及恢复MySQL数据库(Linux 环境)
- gstreamer ffmpeg avdec解码数据流向分析
- 【开发笔记】基于机智云4G转接板GC211的设备上云APP控制
- Beginners use Minio
猜你喜欢

New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears

Take you through the whole process and comprehensively understand the software accidents that belong to testing

The embodiment of generics in inheritance and wildcards

技术干货|关于AI Architecture未来的一些思考

【MindSpore论文精讲】AAAI长尾问题中训练技巧的总结

Summary of Arduino serial functions related to print read

Comparison of advantages and disadvantages between most complete SQL and NoSQL

Paper learning -- Study on the similarity of water level time series of Xingzi station in Poyang Lake

技术干货|昇思MindSpore算子并行+异构并行,使能32卡训练2420亿参数模型

項目經驗分享:實現一個昇思MindSpore 圖層 IR 融合優化 pass
随机推荐
技术干货|利用昇思MindSpore复现ICCV2021 Best Paper Swin Transformer
Use of other streams
Collector in ES (percentile / base)
Segment read
PgSQL converts string to double type (to_number())
Dora (discover offer request recognition) process of obtaining IP address
The difference between typescript let and VaR
Industrial resilience
TypeScript let与var的区别
图像识别与检测--笔记
Common methods of file class
[set theory] order relation (partial order relation | partial order set | example of partial order set)
Vertx restful style web router
gstreamer ffmpeg avdec解码数据流向分析
Spa single page application
The difference between typescript let and VaR
Leetcode 198: 打家劫舍
技术干货|昇思MindSpore NLP模型迁移之Bert模型—文本匹配任务(二):训练和评估
2. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
4everland: the Web3 Developer Center on IPFs has deployed more than 30000 dapps!
https://leetcode.com/problems/merge-two-sorted-lists/