-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathremoveDuplicates.java
More file actions
52 lines (49 loc) · 1.42 KB
/
removeDuplicates.java
File metadata and controls
52 lines (49 loc) · 1.42 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
public class removeDuplicates {
public static void main(String[] args){
generateLinkedList obj = new generateLinkedList();
ListNode a = obj.generate(new int[] {1,1,2,3,4,5,5,5,6});
obj.show(rmv3(a));
obj.show(rmv1(a));
}
/* Approach1: return a new linked list
* */
private static ListNode rmv1(ListNode head){
ListNode dummy = new ListNode(0);
ListNode move = dummy;
while(head != null){
if(head.next == null || (head.val != head.next.val)){
move.next = new ListNode(head.val);
move = move.next;
}
head = head.next;
}
return dummy.next;
}
/* Approach2: remove duplicates in place
* */
private static ListNode rmv2(ListNode head){
ListNode move = head;
while(move != null){
if(move.next != null && move.val == move.next.val){
move.next = move.next.next;
}else{
move = move.next;
}
}
return head;
}
/* Approach3: recursive method
* */
private static ListNode rmv3(ListNode head){
if(head == null || head.next == null){
return head;
}
ListNode nextHead = rmv3(head.next);
if(head.val == nextHead.val){
return nextHead;
}else{
head.next = nextHead;
return head;
}
}
}