-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRemoveDuplicates2.java
44 lines (43 loc) · 1.15 KB
/
RemoveDuplicates2.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
/*https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) return head;
ListNode curr = head;
ListNode prev = head;
ListNode temp = curr.next;
boolean flag = false;
while (temp != null)
{
if (curr.val == temp.val)
{
flag = true;
curr.next = temp.next;
temp = temp.next;
}
else
{
if (flag)
{
curr.val = temp.val;
curr.next = temp.next;
temp = temp.next;
flag = false;
}
else
{
curr = temp;
temp = temp.next;
}
}
}
if (flag)
{
temp = head;
if (temp == curr) return null;
while (temp.next != curr)
temp = temp.next;
temp.next = null;
}
return head;
}
}