-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLeetCode82.java
More file actions
64 lines (61 loc) · 1.82 KB
/
LeetCode82.java
File metadata and controls
64 lines (61 loc) · 1.82 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
64
package problems;
public class LeetCode82 {
public ListNode deleteDuplicates(ListNode head) {
if(head == null){
return head;
}
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;
}
}
class LeetCode82_1 {
public ListNode deleteDuplicates(ListNode head) {
ListNode dummyNode = new ListNode(0, head);
ListNode cur = dummyNode;
if(head == null || head.next == null) {
return head;
}
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 dummyNode.next;
}
}
class LeetCode82_2 {
public ListNode deleteDuplicates(ListNode head) {
if(head == null) {
return head;
}
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;
}
}