-
Notifications
You must be signed in to change notification settings - Fork 31
/
RemoveDuplicates2.java
46 lines (38 loc) · 1.02 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
45
46
package LinkedLists;
/**
* Author - archit.s
* Date - 18/10/18
* Time - 11:53 AM
*/
public class RemoveDuplicates2 {
public ListNode deleteDuplicates(ListNode A) {
ListNode head = null;
ListNode last = null;
ListNode temp = A;
while(temp!=null){
ListNode prev = null;
while(temp.next != null && temp.val == temp.next.val){
prev = temp;
temp = temp.next;
}
if(prev != null && prev.val == temp.val){
temp = temp.next;
}
if(temp!=null && (temp.next == null || (temp.val != temp.next.val))){
if(head == null){
head = temp;
last = temp;
}
else{
last.next = temp;
last = last.next;
}
temp = temp.next;
}
}
if(last != null){
last.next = null;
}
return head;
}
}