Skip to content

Instantly share code, notes, and snippets.

Last active August 29, 2018 16:16
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Remove duplicate nodes with the use of a HashMap array
// Node class
static class Node {
int data;
Node next;
public Node(int data){ = data; = null;
static Node removeDuplicates(Node head){
// null check here
if(head == null || == null) return head;
// create a temporary node
Node next = head;
// create HashMap array
HashMap<Integer, Integer> map = new HashMap<>();
// insert node in map
map.put(, 1);
// iterate list and insert rest of nodes in map
while( != null){
// check if node already exists in map
map.put(, 1);
next =;
} else { =;
// return the head of list
return head;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment