-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathL0082_RemoveDuplicatesFromSortedListII.java
124 lines (111 loc) · 3.93 KB
/
L0082_RemoveDuplicatesFromSortedListII.java
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* https://leetcode.cn/problems/remove-duplicates-from-sorted-list-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
* - 题目数据保证链表已经按升序排列
*/
public class L0082_RemoveDuplicatesFromSortedListII {
// 链表节点定义
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 ListNode deleteDuplicates(ListNode head) {
// 处理空链表和只有一个节点的情况
if (head == null || head.next == null) {
return head;
}
// 创建一个哑节点,处理头节点可能被删除的情况
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode prev = dummy;
ListNode curr = head;
while (curr != null && curr.next != null) {
// 初始化标记,判断当前节点是否是重复节点
boolean isDuplicate = false;
// 如果当前节点与下一个节点值相同,向后移动直到找到不同的值
while (curr.next != null && curr.val == curr.next.val) {
isDuplicate = true;
curr = curr.next;
}
if (isDuplicate) {
// 如果有重复,prev 保持不变,curr 向后移动一位
prev.next = curr.next;
} else {
// 如果没有重复,prev 和 curr 都向后移动
prev = curr;
}
curr = curr.next;
}
return dummy.next;
}
// 辅助方法:创建链表
private static ListNode createList(int[] arr) {
if (arr == null || arr.length == 0) {
return null;
}
ListNode dummy = new ListNode(0);
ListNode curr = dummy;
for (int val : arr) {
curr.next = new ListNode(val);
curr = curr.next;
}
return dummy.next;
}
// 辅助方法:打印链表
private static void printList(ListNode head) {
ListNode curr = head;
System.out.print("[");
while (curr != null) {
System.out.print(curr.val);
if (curr.next != null) {
System.out.print(",");
}
curr = curr.next;
}
System.out.println("]");
}
public static void main(String[] args) {
L0082_RemoveDuplicatesFromSortedListII solution = new L0082_RemoveDuplicatesFromSortedListII();
// 测试用例 1
System.out.println("测试用例 1:");
ListNode head1 = createList(new int[]{1, 2, 3, 3, 4, 4, 5});
System.out.print("输入: ");
printList(head1);
ListNode result1 = solution.deleteDuplicates(head1);
System.out.print("输出: ");
printList(result1);
System.out.println();
// 测试用例 2
System.out.println("测试用例 2:");
ListNode head2 = createList(new int[]{1, 1, 1, 2, 3});
System.out.print("输入: ");
printList(head2);
ListNode result2 = solution.deleteDuplicates(head2);
System.out.print("输出: ");
printList(result2);
System.out.println();
// 测试用例 3:空链表
System.out.println("测试用例 3 (空链表):");
ListNode head3 = null;
System.out.println("输入: []");
ListNode result3 = solution.deleteDuplicates(head3);
System.out.print("输出: ");
printList(result3);
}
}