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

欢迎有更好方法的同学同学评论区踢我(。・ω・。)ノ
边栏推荐
- Industrial resilience
- sharepoint 2007 versions
- Mail sending of vertx
- Epoll related references
- Pgadmin 4 v6.11 release, PostgreSQL open source graphical management tool
- II. D3.js draw a simple figure -- circle
- 【MySQL 14】使用DBeaver工具远程备份及恢复MySQL数据库(Linux 环境)
- [set theory] Stirling subset number (Stirling subset number concept | ball model | Stirling subset number recurrence formula | binary relationship refinement relationship of division)
- New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears
- Technology dry goods | luxe model for the migration of mindspore NLP model -- reading comprehension task
猜你喜欢

New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears
![[coppeliasim4.3] C calls UR5 in the remoteapi control scenario](/img/ca/2f72ea3590c358a6c9884aaa1a1c33.png)
[coppeliasim4.3] C calls UR5 in the remoteapi control scenario

How long is the fastest time you can develop data API? One minute is enough for me

带你全流程,全方位的了解属于测试的软件事故

技术干货|昇思MindSpore NLP模型迁移之LUKE模型——阅读理解任务

Lucene skip table

Analysis of the eighth Blue Bridge Cup single chip microcomputer provincial competition

Dora (discover offer request recognition) process of obtaining IP address

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

The embodiment of generics in inheritance and wildcards
随机推荐
圖像識別與檢測--筆記
Understanding of class
Leetcode 213: looting II
Responsive MySQL of vertx
Read config configuration file of vertx
技术干货|AI框架动静态图统一的思考
你开发数据API最快多长时间?我1分钟就足够了
Industrial resilience
Realize the reuse of components with different routing parameters and monitor the changes of routing parameters
New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears
图像识别与检测--笔记
Hnsw introduction and some reference articles in lucene9
HCIA notes
Technology dry goods | luxe model for the migration of mindspore NLP model -- reading comprehension task
The difference between typescript let and VaR
Custom generic structure
Es writing fragment process
技术干货|昇思MindSpore初级课程上线:从基本概念到实操,1小时上手!
Logging log configuration of vertx
Lucene introduces NFA
https://leetcode.com/problems/merge-two-sorted-lists/