Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d4c007b

Browse files
committed
Added remove duplicates in linked list
1 parent 1a61fc0 commit d4c007b

File tree

2 files changed

+127
-35
lines changed

2 files changed

+127
-35
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.ctci.linkedlists;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* @author rampatra
8+
* @since 21/11/2018
9+
*/
10+
public class RemoveDuplicates {
11+
12+
/**
13+
* Removes duplicates in an unsorted linked list by using additional memory
14+
* and two references.
15+
* <p>
16+
* If asked not to use any additional memory then you can
17+
* loop through the list for each node and check for repeated elements but bear
18+
* in mind that the time complexity of this would be O(n^2) where n is the number
19+
* of nodes in the linked list.
20+
*
21+
* @param head reference to the head of the linked list
22+
*/
23+
private static void removeDuplicatesFromUnsortedList(Node head) {
24+
Set<Integer> valuesInList = new HashSet<>();
25+
Node curr = head;
26+
Node prev = curr;
27+
while (curr != null) {
28+
if (valuesInList.contains(curr.val)) {
29+
prev.next = curr.next;
30+
}
31+
valuesInList.add(curr.val);
32+
prev = curr;
33+
curr = curr.next;
34+
}
35+
}
36+
37+
private static void printList(Node head) {
38+
if (head == null) return;
39+
40+
Node curr = head;
41+
while (curr.next != null) {
42+
System.out.print(curr.val + "->");
43+
curr = curr.next;
44+
}
45+
System.out.println(curr.val);
46+
}
47+
48+
public static void main(String[] args) {
49+
Node l1 = new Node(1);
50+
l1.next = new Node(2);
51+
l1.next.next = new Node(3);
52+
l1.next.next.next = new Node(4);
53+
l1.next.next.next.next = new Node(5);
54+
l1.next.next.next.next.next = new Node(5);
55+
System.out.print("With dups: ");
56+
printList(l1);
57+
removeDuplicatesFromUnsortedList(l1);
58+
System.out.print("Without dups: ");
59+
printList(l1);
60+
61+
Node l2 = new Node(1);
62+
l2.next = new Node(1);
63+
l2.next.next = new Node(2);
64+
l2.next.next.next = new Node(3);
65+
l2.next.next.next.next = new Node(4);
66+
l2.next.next.next.next.next = new Node(5);
67+
System.out.print("\nWith dups: ");
68+
printList(l2);
69+
removeDuplicatesFromUnsortedList(l2);
70+
System.out.print("Without dups: ");
71+
printList(l2);
72+
73+
Node l3 = new Node(1);
74+
l3.next = new Node(2);
75+
l3.next.next = new Node(3);
76+
l3.next.next.next = new Node(3);
77+
l3.next.next.next.next = new Node(4);
78+
l3.next.next.next.next.next = new Node(5);
79+
System.out.print("\nWith dups: ");
80+
printList(l3);
81+
removeDuplicatesFromUnsortedList(l3);
82+
System.out.print("Without dups: ");
83+
printList(l3);
84+
}
85+
}
86+
87+
class Node {
88+
int val;
89+
Node next;
90+
91+
Node(int val) {
92+
this.val = val;
93+
}
94+
}

src/main/java/com/rampatra/linkedlists/RemoveDuplicates.java

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import com.rampatra.common.SingleLinkedList;
44
import com.rampatra.common.SingleLinkedNode;
55

6-
import java.util.HashMap;
7-
import java.util.Map;
6+
import java.util.HashSet;
7+
import java.util.Set;
88

99
/**
1010
* Created by IntelliJ IDEA.
@@ -24,21 +24,19 @@ public class RemoveDuplicates {
2424
*/
2525
public static <E extends Comparable<E>> void removeDuplicates(SingleLinkedList<E> list) {
2626
SingleLinkedNode<E> curr = list.getNode(0);
27-
int index = 0;
2827
while (curr != null) {
2928
// inner while loop for removing multiple duplicates
3029
while (curr.next != null && curr.item == curr.next.item) {
31-
list.remove(index + 1);
30+
curr.next = curr.next.next;
3231
}
33-
index++;
3432
curr = curr.next;
3533
}
3634
}
3735

3836
/**
3937
* Removes duplicates from an unsorted linked list.
4038
* <p/>
41-
* This method uses {@link HashMap}, another
39+
* This method uses {@link HashSet}, another
4240
* way is that you can sort it using merge sort and then
4341
* call {@link #removeDuplicates}.
4442
*
@@ -47,48 +45,48 @@ public static <E extends Comparable<E>> void removeDuplicates(SingleLinkedList<E
4745
*/
4846
public static <E extends Comparable<E>> void removeDuplicatesFromUnsortedList(SingleLinkedList<E> list) {
4947
SingleLinkedNode<E> curr = list.getNode(0);
50-
Map<E, Boolean> map = new HashMap<>();
51-
int index = 0;
48+
SingleLinkedNode<E> prev = curr;
49+
Set<E> itemsInList = new HashSet<>();
5250
while (curr != null) {
53-
if (map.get(curr.item) == null) {
54-
map.put(curr.item, true);
55-
index++;
56-
} else {
57-
list.remove(index);
51+
if (itemsInList.contains(curr.item)) {
52+
itemsInList.add(curr.item);
53+
} else { // delete duplicate element
54+
prev.next = curr.next;
5855
}
56+
prev = curr;
5957
curr = curr.next;
6058
}
6159
}
6260

6361
public static void main(String a[]) {
6462
SingleLinkedList<Integer> linkedList = new SingleLinkedList<>();
65-
linkedList.add(00);
66-
linkedList.add(00);
67-
linkedList.add(00);
68-
linkedList.add(11);
69-
linkedList.add(11);
70-
linkedList.add(22);
71-
linkedList.add(22);
72-
linkedList.add(22);
73-
linkedList.add(22);
74-
linkedList.add(33);
75-
linkedList.add(33);
76-
linkedList.add(33);
77-
linkedList.add(33);
63+
linkedList.add(0);
64+
linkedList.add(0);
65+
linkedList.add(0);
66+
linkedList.add(1);
67+
linkedList.add(1);
68+
linkedList.add(2);
69+
linkedList.add(2);
70+
linkedList.add(2);
71+
linkedList.add(2);
72+
linkedList.add(3);
73+
linkedList.add(3);
74+
linkedList.add(3);
75+
linkedList.add(3);
7876
linkedList.printList();
7977
removeDuplicates(linkedList);
8078
linkedList.printList();
8179

8280
SingleLinkedList<Integer> linkedList2 = new SingleLinkedList<>();
83-
linkedList2.add(00);
84-
linkedList2.add(00);
85-
linkedList2.add(22);
86-
linkedList2.add(11);
87-
linkedList2.add(44);
88-
linkedList2.add(22);
89-
linkedList2.add(66);
90-
linkedList2.add(77);
91-
linkedList2.add(66);
81+
linkedList2.add(0);
82+
linkedList2.add(0);
83+
linkedList2.add(2);
84+
linkedList2.add(1);
85+
linkedList2.add(4);
86+
linkedList2.add(2);
87+
linkedList2.add(6);
88+
linkedList2.add(7);
89+
linkedList2.add(6);
9290
linkedList2.printList();
9391
removeDuplicatesFromUnsortedList(linkedList2);
9492
linkedList2.printList();

0 commit comments

Comments
 (0)