-
-
Notifications
You must be signed in to change notification settings - Fork 316
/
RemoveDuplicates.java
88 lines (80 loc) · 2.67 KB
/
RemoveDuplicates.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.ctci.linkedlists;
import java.util.HashSet;
import java.util.Set;
/**
* @author rampatra
* @since 21/11/2018
*/
public class RemoveDuplicates {
/**
* Removes duplicates in an unsorted linked list by using additional memory
* and two references.
* <p>
* If asked not to use any additional memory then you can
* loop through the list for each node and check for repeated elements but bear
* in mind that the time complexity of this would be O(n^2) where n is the number
* of nodes in the linked list.
*
* @param head reference to the head of the linked list
*/
private static void removeDuplicatesFromUnsortedList(Node head) {
Set<Integer> valuesInList = new HashSet<>();
Node curr = head;
Node prev = curr;
while (curr != null) {
if (valuesInList.contains(curr.val)) {
prev.next = curr.next;
}
valuesInList.add(curr.val);
prev = curr;
curr = curr.next;
}
}
public static void main(String[] args) {
Node l1 = new Node(1);
l1.next = new Node(2);
l1.next.next = new Node(3);
l1.next.next.next = new Node(4);
l1.next.next.next.next = new Node(5);
l1.next.next.next.next.next = new Node(5);
System.out.print("With dups: ");
l1.print();
removeDuplicatesFromUnsortedList(l1);
System.out.print("Without dups: ");
l1.print();
Node l2 = new Node(1);
l2.next = new Node(1);
l2.next.next = new Node(2);
l2.next.next.next = new Node(3);
l2.next.next.next.next = new Node(4);
l2.next.next.next.next.next = new Node(5);
System.out.print("\nWith dups: ");
l2.print();
removeDuplicatesFromUnsortedList(l2);
System.out.print("Without dups: ");
l2.print();
Node l3 = new Node(1);
l3.next = new Node(2);
l3.next.next = new Node(3);
l3.next.next.next = new Node(3);
l3.next.next.next.next = new Node(4);
l3.next.next.next.next.next = new Node(5);
System.out.print("\nWith dups: ");
l3.print();
removeDuplicatesFromUnsortedList(l3);
System.out.print("Without dups: ");
l3.print();
Node l4 = new Node(1);
System.out.print("\nWith dups: ");
l4.print();
removeDuplicatesFromUnsortedList(l4);
System.out.print("Without dups: ");
l4.print();
Node l5 = null;
System.out.print("\nWith dups: ");
l5.print();
removeDuplicatesFromUnsortedList(l5);
System.out.print("Without dups: ");
l5.print();
}
}