Open In App

Java Program For Removing All Occurrences Of Duplicates From A Sorted Linked List

Last Updated : 10 Jan, 2022
Like Article

Given a sorted linked list, delete all nodes that have duplicate numbers (all occurrences), leaving only numbers that appear once in the original list. 

Input: 23->28->28->35->49->49->53->53
Output: 23->35

Input: 11->11->11->11->75->75
Output: empty List

Note that this is different from Remove Duplicates From Linked List

The idea is to maintain a pointer (prev) to the node which just previous to the block of nodes we are checking for duplicates. In the first example, the pointer prev would point to 23 while we check for duplicates for node 28. Once we reach the last duplicate node with value 28 (name it current pointer), we can make the next field of prev node to be the next of current and update This would delete the block of nodes with value 28 which has duplicates.


// Java program to remove all occurrences of
// duplicates from a sorted linked list 
// class to create Linked lIst 
class LinkedList{
// Head of linked list 
Node head = null
class Node
    // Value in the node 
    int val; 
    Node next;
    Node(int v)
        // Default value of the next
        // pointer field 
        val = v;
        next = null;
// Function to insert data nodes into
// the Linked List at the front
public void insert(int data)
    Node new_node = new Node(data); = head;
    head = new_node;
// Function to remove all occurrences
// of duplicate elements 
public void removeAllDuplicates()
    // Create a dummy node that acts like 
    // a fake head of list pointing to the 
    // original head
    Node dummy = new Node(0);
    // Dummy node points to the original head = head;
    Node prev = dummy;
    Node current = head;
    while (current != null)
        // Until the current and previous values
        // are same, keep updating current 
        while ( != null &&
               current =;
        // If current has unique value i.e current
        // is not updated, Move the prev pointer
        // to next node
        if ( == current)
            prev =;
        // When current is updated to the last
        // duplicate value of that segment, make
        // prev the next of current
        current =;
    // Update original head to the next of 
    // dummy node 
    head =;
// Function to print the list elements
public void printList()
    Node trav = head;
    if (head == null)
        System.out.print(" List is empty" );
    while (trav != null)
        System.out.print(trav.val + " ");
        trav =;
// Driver code
public static void main(String[] args)
    LinkedList ll = new LinkedList();
    "Before removal of duplicates");
    "After removal of duplicates");


List before removal of duplicates
23 28 28 35 49 49 53 53 
List after removal of duplicates
23 35 

Time Complexity: O(n)
Please refer complete article on Remove all occurrences of duplicates from a sorted Linked List for more details!

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads