forked from mehul-1607/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_82.java
30 lines (27 loc) · 854 Bytes
/
_82.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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.ListNode;
public class _82 {
public static class Solution1 {
public ListNode deleteDuplicates(ListNode head) {
if (head == null) {
return head;
}
ListNode fakeHead = new ListNode(-1);
fakeHead.next = head;
ListNode pre = fakeHead;
ListNode curr = head;
while (curr != null) {
while (curr.next != null && curr.val == curr.next.val) {
curr = curr.next;
}
if (pre.next == curr) {
pre = pre.next;
} else {
pre.next = curr.next;
}
curr = curr.next;
}
return fakeHead.next;
}
}
}