-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDeleteDuplicatedNode.java
61 lines (51 loc) · 1.45 KB
/
DeleteDuplicatedNode.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
package _18;
import structure.ListNode;
public class DeleteDuplicatedNode {
public static ListNode<Integer> deleteDuplication(ListNode<Integer> head) {
if (head == null) {
return null;
}
ListNode<Integer> dummy = new ListNode<Integer>(0);
dummy.next = head;
ListNode<Integer> pre = dummy;
ListNode<Integer> cur = head;
ListNode<Integer> post = cur.next;
boolean isDuplicate = false;
while (post != null) {
if (post.val == cur.val) {
isDuplicate = true;
post = post.next;
} else if (isDuplicate && post.val != cur.val) {
pre.next = post;
// 删除从 cur 到 post 中间的所有节点
while (cur != post) {
ListNode<Integer> p = cur;
cur = cur.next;
p.next = null;
p = null;
}
post = post.next;
isDuplicate = false;
} else {
pre = cur;
cur = post;
post = post.next;
}
}
if (isDuplicate) {
pre.next = null;
}
return dummy.next;
}
public static void main(String[] args) {
ListNode<Integer> head = new ListNode<Integer>(1);
head.next= new ListNode<>(1);
head.next.next = new ListNode<>(2);
head.next.next.next = new ListNode<>(2);
head.next.next.next.next = new ListNode<>(2);
head.next.next.next.next.next = new ListNode<>(3);
System.out.println(head);
head = deleteDuplication(head);
System.out.println(head);
}
}