Skip to content

Instantly share code, notes, and snippets.

@xnorcode
Created May 3, 2018 18:58
  • 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
Embed
What would you like to do?
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