Skip to content

Instantly share code, notes, and snippets.

@xymor
Created April 23, 2018 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xymor/e00ddbf64df55824802786b37fd555ef to your computer and use it in GitHub Desktop.
Save xymor/e00ddbf64df55824802786b37fd555ef to your computer and use it in GitHub Desktop.
hackerrank RemoveDuplicates from linkedlist
/*
Node is defined as
class Node {
int data;
Node next;
}
*/
Node RemoveDuplicates(Node head) {
Node current = head;
while (current != null) {
Node next = current.next;
if (next != null && next.data == current.data) {
current.next = next.next;
} else {
current = current.next;
}
}
return head;
}
Node RemoveDuplicatesUnsorted(Node head) {
SortedSet<Integer> elements = new SortedSet<Integer>();
Node current = head;
while (current != null) {
Node next = current.next;
if (next != null && elements.contains(next.data)) {
current.next = next.next;
} else {
elements.add(current.data);
current = current.next;
}
}
return head;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment