-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path83.java
More file actions
63 lines (57 loc) · 1.51 KB
/
Copy path83.java
File metadata and controls
63 lines (57 loc) · 1.51 KB
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
/**
* Created by QDHL on 2017/11/7.
*/
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode result, fast, slow;
if (head == null) {
return null;
}
result = head;
slow = head;
fast = slow.next;
while (fast != null) {
if (slow.val == fast.val) {
fast = fast.next;
if (fast == null) { //最后两个重复,删除最后一个
slow.next = null;
}
} else {
if (fast == slow.next) {
slow = fast;
} else {
slow.next = fast;
}
}
}
return result;
}
public static void main(String[] args) {
ListNode l1 = new ListNode(1);
ListNode l2 = new ListNode(1);
ListNode l3 = new ListNode(2);
ListNode l4 = new ListNode(2);
ListNode l5 = new ListNode(3);
ListNode l6 = new ListNode(4);
ListNode l7 = new ListNode(5);
ListNode l8 = new ListNode(5);
l1.next = l2;
l2.next = l3;
l3.next = l4;
l4.next = l5;
l5.next = l6;
l6.next = l7;
l7.next = l8;
l8.next = null;
ListNode h = new Solution().deleteDuplicates(l1);
while (h != null) {
System.out.println(h.val);
h = h.next;
}
}
}