82. 删除排序链表中的重复元素II
82. 删除排序链表中的重复元素II
题目
描述:
给定一个已排序的链表的头 head,删除原始链表中所有重复数字的节点,只留下不同的数字。返回 已排序的链表。
示例 1:
- 输入:
head = [1,2,3,3,4,4,5] - 输出:
[1,2,5]
示例 2:
- 输入:
head = [1,1,1,2,3] - 输出:
[2,3]
提示:
- 链表中节点数目在范围
[0, 300]内 -100 <= Node.val <= 100- 题目数据保证链表已经按 升序 排列
题解
按连续的相同值处理
题目要求只保留原链表中出现一次的值。例如 [1,2,2,3] 应得到 [1,3],两个 2 都要删除。由于链表有序,同一个值的节点必然连续,可以一次判断、跳过整段。
添加虚拟头节点 dummy,并维护两个指针:prev 指向已经确认保留的最后一个节点,初始为 dummy;cur 指向下一段尚未处理的首节点,初始为 head。
- 若
cur与cur.next同值,就记住这个值,让cur一直前进到不同值或空节点,再令prev.next = cur,跳过整段。此时prev保持不动,因为下一段也可能重复。 - 若不存在同值后继,说明当前值只出现一次,让
prev = cur保留它,再把cur向后移动一步。
例如 [1,2,3,3,4,4,5]:保留 1、2 后,prev 指向 2;跳过两个 3 后仍停在 2,再跳过两个 4,最终将 2 连到 5,得到 [1,2,5]。
核心代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public ListNode deleteDuplicates(ListNode head) {
ListNode dummy = new ListNode(0, head);
ListNode prev = dummy;
ListNode cur = head;
while (cur != null) {
if (cur.next != null && cur.val == cur.next.val) {
int v = cur.val;
while (cur != null && cur.val == v) {
cur = cur.next;
}
prev.next = cur;
} else {
prev = cur;
cur = cur.next;
}
}
return dummy.next;
}
下面完整示例把两个指针的职责合并:其中的 cur 指向保留段的尾节点,对应上面核心代码中的 prev。它检查的是 cur.next 和 cur.next.next;发现重复后不断修改 cur.next,直到这一段全部被跳过。阅读两版时要注意,同名变量 cur 的含义不同。
虚拟头使开头的重复段也能统一删除。空链表直接返回空;若所有节点都重复,最终 dummy.next 为 null。每个节点最多被向后扫描一次,时间为 $O(n)$,辅助空间为 $O(1)$。
完整示例
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
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 deleteDuplicates(ListNode head) {
if (head == null) {
return null;
}
ListNode dummy = new ListNode(0, head);
ListNode cur = dummy;
while (cur.next != null && cur.next.next != null) {
if (cur.next.val == cur.next.next.val) {
int x = cur.next.val;
while (cur.next != null && cur.next.val == x) {
cur.next = cur.next.next;
}
} else {
cur = cur.next;
}
}
return dummy.next;
}
public static ListNode createList(int[] arr) {
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
for (int val : arr) {
cur.next = new ListNode(val);
cur = cur.next;
}
return dummy.next;
}
public static void printList(ListNode head) {
StringBuilder sb = new StringBuilder();
sb.append("[");
ListNode cur = head;
while (cur != null) {
sb.append(cur.val);
if (cur.next != null) {
sb.append(", ");
}
cur = cur.next;
}
sb.append("]");
System.out.println(sb.toString());
}
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 3, 4, 4, 5};
ListNode head1 = createList(arr1);
System.out.println("示例 1 输入:");
printList(head1);
ListNode res1 = deleteDuplicates(head1);
System.out.println("示例 1 输出:");
printList(res1);
System.out.println("-----------------------------");
int[] arr2 = {1, 1, 1, 2, 3};
ListNode head2 = createList(arr2);
System.out.println("示例 2 输入:");
printList(head2);
ListNode res2 = deleteDuplicates(head2);
System.out.println("示例 2 输出:");
printList(res2);
System.out.println("-----------------------------");
int[] arr3 = {1, 1};
ListNode head3 = createList(arr3);
System.out.println("示例 3 输入:");
printList(head3);
ListNode res3 = deleteDuplicates(head3);
System.out.println("示例 3 输出:");
printList(res3);
}
}
运行结果(OpenJDK 21.0.12):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
示例 1 输入:
[1, 2, 3, 3, 4, 4, 5]
示例 1 输出:
[1, 2, 5]
-----------------------------
示例 2 输入:
[1, 1, 1, 2, 3]
示例 2 输出:
[2, 3]
-----------------------------
示例 3 输入:
[1, 1]
示例 3 输出:
[]
本文由作者按照 CC BY 4.0 进行授权


