Skip to content

Instantly share code, notes, and snippets.

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