Skip to content

Instantly share code, notes, and snippets.

@charlespunk
Created February 10, 2013 20:35
Show Gist options
  • Save charlespunk/4750962 to your computer and use it in GitHub Desktop.
Save charlespunk/4750962 to your computer and use it in GitHub Desktop.
Write code to remove duplicates from an unsorted linked list.
FOLLOW UP
How would you solve this problem if a temporary buffer is not allowed?
public static void removeDuplicateNode(Node root){
while(root != null){
Node thisNode = root;
while(thisNode.next != null){
if(thisNode.next.data == root.data) thisNode.next = thisNode.next.next;
//be careful of this 'else'
else thisNode = thisNode.next;
}
root = root.next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment