Created
April 23, 2018 14:43
-
-
Save xymor/e00ddbf64df55824802786b37fd555ef to your computer and use it in GitHub Desktop.
hackerrank RemoveDuplicates from linkedlist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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