21. 合并两个有序链表
21. 合并两个有序链表
题目
描述:
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例 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均按 非递减顺序 排列
题解
每次连接两个剩余链表中较小的头节点
两个链表均按非递减顺序排列,各自剩余部分的最小值就在头节点。所以比较当前 l1.val 和 l2.val,把较小节点连接到结果尾部,再推进它所属链表的引用,就能逐步构造有序结果。值相同时任选一侧均可,代码用 <= 优先取第一条链表的节点。
例如 [1,2,4] 与 [1,3,4],依次取第一条的 1、第二条的 1、2、3,再处理两个 4。每次选中的都是全部未处理节点中的最小值,因此已连接前缀始终有序。
结果链表从哑节点 dummy 开始,curr 指向结果尾部。没有哑节点时,第一次连接需要单独确定结果的头节点;有了它,每一步都统一为 curr.next = 较小节点; curr = curr.next。
一条链表走完后,另一条剩下的部分本来就有序,且都不小于已连接的节点,直接整段挂到结果尾部,不需要再逐个比较。
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode curr = dummy;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
curr.next = l1;
l1 = l1.next;
} else {
curr.next = l2;
l2 = l2.next;
}
curr = curr.next;
}
curr.next = (l1 != null) ? l1 : l2;
return dummy.next;
}
设两链长度为 m、n,最坏扫描线性数量的节点,时间为 $O(m+n)$。除辅助头节点和几个引用外,不创建结果节点,辅助空间为 $O(1)$。任一链为空时循环不执行,直接连接另一条;两链都为空时返回 null。
这个实现复用原节点并改写它们的 next,得到的结果与输入共享节点。它不是一份独立副本;如果后续需要保留原链结构,应采用创建新节点的归并方式,并另行计入空间。
完整示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
public class Main {
public static 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 static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(-1);
ListNode curr = dummy;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
curr.next = l1;
l1 = l1.next;
} else {
curr.next = l2;
l2 = l2.next;
}
curr = curr.next;
}
curr.next = (l1 != null) ? l1 : l2;
return dummy.next;
}
public static ListNode createList(int[] arr) {
ListNode dummy = new ListNode(-1);
ListNode curr = dummy;
for (int val : arr) {
curr.next = new ListNode(val);
curr = curr.next;
}
return dummy.next;
}
public static void printList(ListNode head) {
StringBuilder sb = new StringBuilder();
sb.append("[");
while (head != null) {
sb.append(head.val);
if (head.next != null) {
sb.append(", ");
}
head = head.next;
}
sb.append("]");
System.out.println(sb.toString());
}
public static void main(String[] args) {
int[] arr1 = {1, 2, 4};
int[] arr2 = {1, 3, 4};
ListNode l1 = createList(arr1);
ListNode l2 = createList(arr2);
System.out.println("示例 1 输入:");
System.out.print("l1 = ");
printList(l1);
System.out.print("l2 = ");
printList(l2);
ListNode res1 = mergeTwoLists(l1, l2);
System.out.print("示例 1 输出: ");
printList(res1);
System.out.println("-----------------------------");
int[] arr3 = {};
int[] arr4 = {};
ListNode l3 = createList(arr3);
ListNode l4 = createList(arr4);
System.out.println("示例 2 输入:");
System.out.print("l1 = ");
printList(l3);
System.out.print("l2 = ");
printList(l4);
ListNode res2 = mergeTwoLists(l3, l4);
System.out.print("示例 2 输出: ");
printList(res2);
System.out.println("-----------------------------");
int[] arr5 = {};
int[] arr6 = {0};
ListNode l5 = createList(arr5);
ListNode l6 = createList(arr6);
System.out.println("示例 3 输入:");
System.out.print("l1 = ");
printList(l5);
System.out.print("l2 = ");
printList(l6);
ListNode res3 = mergeTwoLists(l5, l6);
System.out.print("示例 3 输出: ");
printList(res3);
}
}
运行结果(OpenJDK 21.0.12):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
示例 1 输入:
l1 = [1, 2, 4]
l2 = [1, 3, 4]
示例 1 输出: [1, 1, 2, 3, 4, 4]
-----------------------------
示例 2 输入:
l1 = []
l2 = []
示例 2 输出: []
-----------------------------
示例 3 输入:
l1 = []
l2 = [0]
示例 3 输出: [0]
本文由作者按照 CC BY 4.0 进行授权

