-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRemove Duplicated From Sorted List 2.java
More file actions
69 lines (46 loc) · 1.59 KB
/
Remove Duplicated From Sorted List 2.java
File metadata and controls
69 lines (46 loc) · 1.59 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
65
66
67
68
69
//https://www.interviewbit.com/problems/remove-duplicates-from-sorted-list-ii/
/**
* Definition for singly-linked list.
* class ListNode {
* public int val;
* public ListNode next;
* ListNode(int x) { val = x; next = null; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode A) {
//remove from starting
ListNode a = A;
while(true){
if(a.next != null && a.val == a.next.val){
int q = a.val;
ListNode p = a;
while(p != null && p.val == q)
p = p.next;
if(p == null) return null;
a = p;
} else
break;
}
//do for middle
A = a;
if(a == null) return null;
a = a.next;
if(a == null) return A;
ListNode prev = A;
while(a!= null && prev != null){
while(a != null && a.next != null && a.val == a.next.val){
int data = a.val;
ListNode link = a;
while(link != null && link.val == data)
link = link.next;
prev.next = link;
a = link;
}
if(a == null) break;
prev = prev.next;
a = a.next;
}
return A;
}
}