Skip to content

Commit

Permalink
Adding to dedup linked list
Browse files Browse the repository at this point in the history
  • Loading branch information
vprusso committed Jul 21, 2019
1 parent 367653f commit 5beac7b
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Given a list of bytes `a`, each representing one byte of a larger integer (ie. `
Given k sorted arrays, merge them into a single sorted array.

* [Dedup Linked List](http://www.byte-by-byte.com/deduplinkedlist/)
([Java](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/DedupLinkedList.java))
([Python](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/python/DedupLinkedList.py))<br>
Given an unsorted linked list, write a function to remove all the duplicates.

Expand Down
109 changes: 109 additions & 0 deletions java/DedupLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Title: Remove duplicates of linked list
*
* Given an unsorted linked list, write a function to remove all the duplicates.
*
* Execution: javac DedupLinkedList.java && java DedupLinkedList
*
* For more details, check out http://www.byte-by-byte.com/deduplinkedlist
*/
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;


public class DedupLinkedList {
// Private node class
private static class Node {
private int value;
private Node next;

private Node(int value) {
this.value = value;
}
}

public static List<Integer> printList(Node n) {
List<Integer> list = new ArrayList<>();
while (n != null) {
list.add(n.value);
n = n.next;
}
return list;
}

public static void removeDups(Node n) {
HashSet<Integer> nodes = new HashSet<Integer>();
Node prev = null;

while (n != null) {
if (nodes.contains(n.value)) {
prev.next = n.next;
} else {
nodes.add(n.value);
prev = n;
}
n = n.next;
}
prev.next = null;
}

// Sample test cases
public static void main(String[] args) {

// Test 1 duplicate:
Node n = new Node(1);
n.next = new Node(2);
n.next.next = new Node(3);
n.next.next.next = new Node(4);
n.next.next.next.next = new Node(4);

removeDups(n);
List<Integer> l1 = printList(n);
int[] ints = {1,2,3,4};
List<Integer> l2 = Arrays.stream(ints).boxed().collect(Collectors.toList());

if (l1.equals(l2))
System.out.println("Test with 1 duplicate passed.");
else
System.out.println("Test with 1 duplicate failed");

// Test 2 duplicates
Node n2 = new Node(1);
n2.next = new Node(1);
n2.next.next = new Node(2);
n2.next.next.next = new Node(2);
n2.next.next.next.next = new Node(3);

removeDups(n2);
List<Integer> l1_2 = printList(n2);
int[] ints2 = {1,2,3};
List<Integer> l2_2 = Arrays.stream(ints2).boxed().collect(Collectors.toList());

if (l1_2.equals(l2_2))
System.out.println("Test with 2 duplicates passed.");
else
System.out.println("Test with 2 duplicates failed.");

// Test no duplicates
Node n3 = new Node(1);
n3.next = new Node(2);
n3.next.next = new Node(3);
n3.next.next.next = new Node(4);
n3.next.next.next.next = new Node(5);

removeDups(n3);
List<Integer> l1_3 = printList(n3);
int[] ints3 = {1,2,3,4,5};
List<Integer> l2_3 = Arrays.stream(ints3).boxed().collect(Collectors.toList());

if (l1_3.equals(l2_3))
System.out.println("Test with no duplicates passed.");
else
System.out.println("Test with no duplicates failed.");


}
}

0 comments on commit 5beac7b

Please sign in to comment.