Skip to content

Instantly share code, notes, and snippets.

@xnorcode
Created May 3, 2018 18:58
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 xnorcode/1567cf0823e6684a9e3536e11ff01673 to your computer and use it in GitHub Desktop.
Save xnorcode/1567cf0823e6684a9e3536e11ff01673 to your computer and use it in GitHub Desktop.
Linked List Remove Duplicate Nodes
public class LinkedList<T> {
// Reference to the head node
Node head;
public void removeDuplicates(){
// check if list empty
if(head == null) return;
// chech if single node
if(head.next == null) return;
// get head reference
Node tmp = head;
// iterate list
while(tmp.next != null){
if(tmp.data == tmp.next.data){
// if duplicate remove and check with next
tmp.next = tmp.next.next;
} else {
// or go to next node
tmp = tmp.next;
}
}
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment