-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode83.java
More file actions
42 lines (38 loc) · 1.28 KB
/
LeetCode83.java
File metadata and controls
42 lines (38 loc) · 1.28 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
import util.ListNode;
import util.PrintUtil;
public class LeetCode83 {
public static void main(String[] args) {
// 输入:head = [1,1,2]
// 输出:[1,2]
System.out.println(new Solution83().deleteDuplicates(ListNode.buildLinkedList(new Integer[] { 1, 1, 2 })));
PrintUtil.printDivider();
// 输入:head = [1,1,2,3,3]
// 输出:[1,2,3]
System.out
.println(new Solution83().deleteDuplicates(ListNode.buildLinkedList(new Integer[] { 1, 1, 2, 3, 3 })));
PrintUtil.printDivider();
// 输入:head = [1,1,1]
// 输出:[1]
System.out
.println(new Solution83().deleteDuplicates(ListNode.buildLinkedList(new Integer[] { 1, 1, 1 })));
PrintUtil.printDivider();
}
}
class Solution83 {
public ListNode deleteDuplicates(ListNode head) {
ListNode node = head;
while (node != null && node.next != null) {
if (node.next.val == node.val) {
if (node.next.next != null) {
node.next = node.next.next;
} else {
node.next = null;
break;
}
} else {
node = node.next;
}
}
return head;
}
}