Remove duplicates from sorted linked list

Last Updated : 9 Oct, 2026

Given the head of a sorted singly linked list, remove all duplicate nodes so that each element appears only once. The resulting linked list should remain sorted.

Note: Try to solve the problem without using extra space

Examples:

Input: Head: 2->2->4->5
Output: 2 -> 4 -> 5

11

Explanation: In the given linked list 2 -> 2 -> 4 -> 5, only 2 occurs more than 1 time. So we need to remove it once.

Input: Head: 2->2->2->2->2
Output: 2

12

Explanation: In the given linked list 2 -> 2 -> 2 -> 2, 2 is the only element and is repeated 5 times. So we need to remove any four 2.

Try It Yourself
redirect icon

[Naive Approach] Hash Set – O(n) Time and O(n) Space

The idea is to traverse the linked list and check if the value is present in Hash Set or not.

If the value is not present in the Hash Set then push the value in Hash Set append the nodes in the new list , otherwise skip the value as it is the duplicate value.

Working of Approach:

  • Initialize an empty Hash Set and pointers new_head and tail as NULL.
  • Iterate through the original list, adding each unique node's value to the Hash Set and appending the node to the new list.
  • Return the new_head of the new list with duplicates removed.
C++
#include <iostream>
using namespace std;

class Node
{
  public:
    int data;
    Node *next;
    Node(int x)
    {
        data = x;
        next = nullptr;
    }
};

Node *removeDuplicates(Node *head)
{

    // Unordered map to track unique node values
    unordered_set<int> st;

    // Initialize pointers for traversing the original list
    // and building the new list without duplicates
    Node *new_head = nullptr;
    Node *tail = nullptr;

    // Traverse the original list
    Node *curr = head;
    while (curr != nullptr)
    {

        // Check if the current node's data is not in the map
        if (st.find(curr->data) == st.end())
        {

            // Create a new node for the unique data
            Node *new_node = new Node(curr->data);

            // If new_head is null, this is the
            // first unique node
            if (new_head == nullptr)
            {
                new_head = new_node;
                tail = new_head;
            }
            else
            {
                // Append the new node to the end
                // of the new list
                tail->next = new_node;
                tail = new_node;
            }

            // Mark this data as encountered
            st.insert(curr->data);
        }

        // Move to the next node in the original list
        curr = curr->next;
    }

    // delete old list
    curr = head;
    while (curr != nullptr)
    {
        Node *temp = curr;
        curr = curr->next;
        delete temp;
    }

    // Return the head of the new list with
    // duplicates removed
    return new_head;
}

void printList(Node *node)
{
    while (node != NULL)
    {
        cout << node->data << " ";
        node = node->next;
    }
    cout << endl;
}

int main()
{

    // Create a sorted linked list
    // 11->11->11->13->13->20
    Node *head = new Node(11);
    head->next = new Node(11);
    head->next->next = new Node(11);
    head->next->next->next = new Node(13);
    head->next->next->next->next = new Node(13);
    head->next->next->next->next->next = new Node(20);

    printList(head);

    head = removeDuplicates(head);

    printList(head);

    return 0;
}
Java
import java.io.*;
import java.util.HashSet;

class Node {
    int data;
    Node next;
    Node(int x)
    {
        data = x;
        next = null;
    }
}

class GFG {

    // Function to remove duplicates
    static Node removeDuplicates(Node head)
    {

        // HashSet to track unique node values
        HashSet<Integer> st = new HashSet<>();

        // Initialize pointers for traversing
        // the original list and building the new
        // list without duplicates
        Node temp = head;
        Node newHead = null;
        Node tail = null;

        // Traverse the original list
        while (temp != null) {

            // Check if the current node's data is not in
            // the set
            if (!st.contains(temp.data)) {

                // Create a new node for the unique data
                Node newNode = new Node(temp.data);

                // If newHead is null, this is the first
                // unique node
                if (newHead == null) {
                    newHead = newNode;
                    tail = newHead;
                }
                else {
                    // Append the new node to the
                    // end of the new list
                    tail.next = newNode;
                    tail = newNode;
                }

                // Mark this data as encountered
                st.add(temp.data);
            }
            // Move to the next node in the original list
            temp = temp.next;
        }

        // Return the head of the new list with
        // duplicates removed
        return newHead;
    }

    // Function to print nodes in a given linked list
    public static void printList(Node node)
    {
        while (node != null) {
            System.out.print(node.data + " ");
            node = node.next;
        }
        System.out.println();
    }

    public static void main(String[] args)
    {

        // Create a sorted linked list:
        // 11->11->11->13->13->20
        Node head = new Node(11);
        head.next = new Node(11);
        head.next.next = new Node(11);
        head.next.next.next = new Node(13);
        head.next.next.next.next = new Node(13);
        head.next.next.next.next.next = new Node(20);

        printList(head);

        head = removeDuplicates(head);

        printList(head);
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None


def removeDuplicates(head):

  # Set to track unique node values
    st = set()

    # Initialize pointers for traversing
    # the original list and building the 
    # new list without duplicates
    temp = head
    new_head = None
    tail = None

    # Traverse the original list
    while temp:

        # Check if the current node's data
        # is not in the set
        if temp.data not in st:

            # Create a new node for the unique data
            new_node = Node(temp.data)

            # If new_head is None, this is the 
            #first unique node
            if new_head is None:
                new_head = new_node
                tail = new_head
            else:
                # Append the new node to the end
                #of the new list
                tail.next = new_node
                tail = new_node

            # Mark this data as encountered
            st.add(temp.data)

        # Move to the next node in the original list
        temp = temp.next

    # Return the head of the new list with
    #duplicates removed
    return new_head


def print_list(node):
    while node:
        print(node.data, end=" ")
        node = node.next
    print()


if __name__ == "__main__":
  
    # Create a sorted linked list:
    # 11->11->11->13->13->20
    head = Node(11)
    head.next = Node(11)
    head.next.next = Node(11)
    head.next.next.next = Node(13)
    head.next.next.next.next = Node(13)
    head.next.next.next.next.next = Node(20)

    print_list(head)

    head = removeDuplicates(head)

    print_list(head)
C#
using System;
using System.Collections.Generic;

public class Node {
    public int data;
    public Node next;
    public Node(int x)
    {
        data = x;
        next = null;
    }
}

class GFG {

    // Function to remove duplicates
    static Node removeDuplicates(Node head)
    {

        // HashSet to track unique node values
        HashSet<int> st = new HashSet<int>();

        // Initialize pointers for traversing the original
        // list and building the new list without duplicates
        Node temp = head;
        Node newHead = null;
        Node tail = null;

        // Traverse the original list
        while (temp != null) {

            // Check if the current node's data
            // is not in the set
            if (!st.Contains(temp.data)) {

                // Create a new node for the unique data
                Node newNode = new Node(temp.data);

                // If newHead is null, this is the
                // first unique node
                if (newHead == null) {
                    newHead = newNode;
                    tail = newHead;
                }
                else {
                    // Append the new node to the end
                    // of the new list
                    tail.next = newNode;
                    tail = newNode;
                }

                // Mark this data as encountered
                st.Add(temp.data);
            }
            // Move to the next node in the
            // original list
            temp = temp.next;
        }
        // Return the head of the new list
        return newHead;
    }

    static void PrintList(Node node)
    {
        while (node != null) {
            Console.Write(node.data + " ");
            node = node.next;
        }
        Console.WriteLine();
    }

    static void Main()
    {

        // Create a sorted linked list:
        // 11->11->11->13->13->20
        Node head = new Node(11);
        head.next = new Node(11);
        head.next.next = new Node(11);
        head.next.next.next = new Node(13);
        head.next.next.next.next = new Node(13);
        head.next.next.next.next.next = new Node(20);

        PrintList(head);

        head = removeDuplicates(head);

        PrintList(head);
    }
}
JavaScript
class Node {
    constructor(x)
    {
        this.data = x;
        this.next = null;
    }
}

function removeDuplicates(head)
{

    // Set to track unique node values
    const st = new Set();

    // Initialize pointers for traversing the original list
    // and building the new list without duplicates
    let temp = head;
    let newHead = null;
    let tail = null;

    // Traverse the original list
    while (temp !== null) {

        // Check if the current node's
        // data is not in the set
        if (!st.has(temp.data)) {

            // Create a new node for the unique data
            const newNode = new Node(temp.data);

            // If newHead is null, this is the first
            // unique node
            if (newHead === null) {
                newHead = newNode;
                tail = newHead;
            }
            else {
                // Append the new node to the end of
                // the new list
                tail.next = newNode;
                tail = newNode;
            }

            // Mark this data as encountered
            st.add(temp.data);
        }
        // Move to the next node in the original list
        temp = temp.next;
    }

    // Return the head of the new list with
    // duplicates removed
    return newHead;
}

function printList(node)
{
    let current = node;
    while (current) {
        process.stdout.write(current.data + " ");
        current = current.next;
    }
    console.log();
}

// Create a sorted linked list:
// 11->11->11->13->13->20
let head = new Node(11);
head.next = new Node(11);
head.next.next = new Node(11);
head.next.next.next = new Node(13);
head.next.next.next.next = new Node(13);
head.next.next.next.next.next = new Node(20);

printList(head);

head = removeDuplicates(head);

printList(head);

Output
11 11 11 13 13 20 
11 13 20 

[Expected Approach] Changing Next Pointers – O(n) Time and O(1) Space

The idea is to traverse the linked list and for each node, if the next node has the same data, skip and delete the duplicate node.

Working of Approach:

  • Traverse the linked list starting from the head node.
  • Iterate through the list, comparing each node with the next node.
  • If the data in the next node is same as the curr node adjust pointers to skip the next node.
C++
#include <bits/stdc++.h>
using namespace std;

class Node
{
  public:
    int data;
    Node *next;
    Node(int x)
    {
        data = x;
        next = nullptr;
    }
};

Node *removeDuplicates(Node *head)
{
    Node *curr = head;

    // Traverse the list
    while (curr != NULL && curr->next != NULL)
    {

        // Check if next value is same as current
        if (curr->data == curr->next->data)
        {
            Node *temp = curr->next;
            curr->next = curr->next->next;
            delete temp;
        }
        else
            curr = curr->next;
    }
    return head;
}

int main()
{

    // Create a sorted linked list
    // 11->11->11->13->13->20
    Node *head = new Node(11);
    head->next = new Node(11);
    head->next->next = new Node(11);
    head->next->next->next = new Node(13);
    head->next->next->next->next = new Node(13);
    head->next->next->next->next->next = new Node(20);

    // Original list
    Node *temp = head;
    while (temp != NULL)
    {
        cout << temp->data << " ";
        temp = temp->next;
    }
    cout << endl;

    head = removeDuplicates(head);

    // list withn no duplicates
    temp = head;
    while (temp != NULL)
    {
        cout << temp->data << " ";
        temp = temp->next;
    }

    return 0;
}
C
#include <stdio.h>

struct Node {
    int data;
    struct Node *next;
};

// Function to remove duplicates
struct Node *removeDuplicates(struct Node *head) {
    struct Node *curr = head;

    // Traverse the list
    while (curr != NULL && curr->next != NULL) {
      
        // Check if next value is the same as curr
        if (curr->data == curr->next->data) {
            struct Node* temp = curr->next;
            curr->next = curr->next->next;
            free(temp);
        }
        else
            curr = curr->next;
    }
    return head;
}

struct Node *createNode(int new_data){
    struct Node *new_node =
      (struct Node *)malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = NULL;
    return new_node;
}

int main() {
  
    // Create a sorted linked list:
    // 11->11->11->13->13->20
    struct Node *head = createNode(11);
    head->next = createNode(11);
    head->next->next = createNode(11);
    head->next->next->next = createNode(13);
    head->next->next->next->next = createNode(13);
    head->next->next->next->next->next = createNode(20);

    // original list
    struct Node *temp = head;
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");

    head = removeDuplicates(head);

    // list with no duplicates
    temp = head;
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");

    return 0;
}
Java
import java.io.*;

class Node {
    int data;
    Node next;
    Node(int x)
    {
        data = x;
        next = null;
    }
}

class GFG {

    // Function to remove duplicates
    static Node removeDuplicates(Node head)
    {
        Node curr = head;

        // Traverse the list
        while (curr != null && curr.next != null) {

            // Check if next value is the same as curr
            if (curr.data == curr.next.data) {
                Node nextNext = curr.next.next;
                curr.next = nextNext;
            }
            else {
                curr = curr.next;
            }
        }
        return head;
    }

    // Driver code
    public static void main(String[] args)
    {

        // Create a sorted linked list:
        // 11->11->11->13->13->20
        Node head = new Node(11);
        head.next = new Node(11);
        head.next.next = new Node(11);
        head.next.next.next = new Node(13);
        head.next.next.next.next = new Node(13);
        head.next.next.next.next.next = new Node(20);

        Node temp = head;
        while (temp != null) {
            System.out.print(temp.data + " ");
            temp = temp.next;
        }
        System.out.println();

        head = removeDuplicates(head);

        // listwith no duplicate
        temp = head;
        while (temp != null) {
            System.out.print(temp.data + " ");
            temp = temp.next;
        }
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None


def removeDuplicates(head):
    curr = head

    # Traverse the list
    while curr and curr.next:
      
        # Check if next value is the same as curr
        if curr.data == curr.next.data:
            next_next = curr.next.next
            curr.next = next_next
        else:
            curr = curr.next

    return head

# Driver code
if __name__ == "__main__":
  
    # Create a sorted linked list:
    # 11->11->11->13->13->20
    head = Node(11)
    head.next = Node(11)
    head.next.next = Node(11)
    head.next.next.next = Node(13)
    head.next.next.next.next = Node(13)
    head.next.next.next.next.next = Node(20)

    # original list
    temp = head
    while temp:
        print(temp.data, end=" ")
        temp = temp.next
    print()

    head = removeDuplicates(head)

    # list with no duplicate
    temp = head
    while temp:
        print(temp.data, end=" ")
        temp = temp.next
    print()
C#
using System;

public class Node {
    public int data;
    public Node next;
    public Node(int x)
    {
        data = x;
        next = null;
    }
}

class GFG {

    // Function to remove duplicates
    static Node removeDuplicates(Node head)
    {
        Node curr = head;

        // Traverse the list
        while (curr != null && curr.next != null) {

            // Check if next value is the same as curr
            if (curr.data == curr.next.data) {
                Node nextNext = curr.next.next;
                curr.next = nextNext;
            }
            else {
                curr = curr.next;
            }
        }
        return head;
    }

    // Driver code
    static void Main()
    {

        // Create a sorted linked list:
        // 11->11->11->13->13->20
        Node head = new Node(11);
        head.next = new Node(11);
        head.next.next = new Node(11);
        head.next.next.next = new Node(13);
        head.next.next.next.next = new Node(13);
        head.next.next.next.next.next = new Node(20);

        // original list
        Node temp = head;
        while (temp != null) {
            Console.Write(temp.data + " ");
            temp = temp.next;
        }
        Console.WriteLine();

        head = removeDuplicates(head);

        // list with no duplicates
        temp = head;
        while (temp != null) {
            Console.Write(temp.data + " ");
            temp = temp.next;
        }
        Console.WriteLine();
    }
}
JavaScript
class Node {
    constructor(x)
    {
        this.data = x;
        this.next = null;
    }
}

function removeDuplicates(head)
{
    let curr = head;

    // Traverse the list
    while (curr && curr.next) {

        // Check if next value is the same as curr
        if (curr.data === curr.next.data) {
            let nextNext = curr.next.next;
            curr.next = nextNext;
        }
        else {
            curr = curr.next;
        }
    }

    return head;
}

// Driver code
// Create a sorted linked list:
// 11->11->11->13->13->20
let head = new Node(11);
head.next = new Node(11);
head.next.next = new Node(11);
head.next.next.next = new Node(13);
head.next.next.next.next = new Node(13);
head.next.next.next.next.next = new Node(20);

// original list
let temp = head;
while (temp !== null) {
    process.stdout.write(temp.data + " ");
    temp = temp.next;
}
console.log();

head = removeDuplicates(head);

// list with no duplicates
temp = head;
while (temp !== null) {
    process.stdout.write(temp.data + " ");
    temp = temp.next;
}

Output
11 11 11 13 13 20 
11 13 20 

[Alternate Approach] Recursion – O(n) Time and O(n) Space

The idea is similar to the iterative approach. Here we are using the recursion to check each node and its next for duplicates.

Please note that the iterative approach would be better in terns of time and space. The recursive approach can be good fun exercise or a question in an interview / exam.

Working of Approach:

  • If the curr node or its next node is NULL, return the curr node.
  • If the current node’s data equals the next node’s data, adjust pointers to skip the duplicate.
  • If no duplicate, recursively process the next node.
C++
#include <iostream>
using namespace std;

class Node
{
  public:
    int data;
    Node *next;

    Node(int new_data)
    {
        data = new_data;
        next = nullptr;
    }
};

Node *removeDuplicates(Node *head)
{
    if (head == NULL)
        return head;

    if (head->next != NULL)
    {
        if (head->data == head->next->data)
        {
            Node *temp = head->next;
            head->next = head->next->next;
            delete temp;

            return removeDuplicates(head);
        }
        else
        {
            removeDuplicates(head->next);
        }
    }

    return head;
}

int main()
{
    // Create a sorted linked list
    // 11->11->11->13->13->20
    Node *head = new Node(11);
    head->next = new Node(11);
    head->next->next = new Node(11);
    head->next->next->next = new Node(13);
    head->next->next->next->next = new Node(13);
    head->next->next->next->next->next = new Node(20);

    // Printing old list
    Node *temp = head;
    while (temp != NULL)
    {
        cout << temp->data << " ";
        temp = temp->next;
    }
    cout << endl;

    removeDuplicates(head);

    // Printing new list
    temp = head;
    while (temp != NULL)
    {
        cout << temp->data << " ";
        temp = temp->next;
    }

    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
    int data;
    struct Node *next;
} Node;

Node *removeDuplicates(Node *head)
{
    if (head == NULL)
        return head;

    if (head->next != NULL)
    {
        if (head->data == head->next->data)
        {
            Node *temp = head->next;
            head->next = head->next->next;
            free(temp);

            return removeDuplicates(head);
        }
        else
        {
            removeDuplicates(head->next);
        }
    }

    return head;
}

int main()
{
    // Create a sorted linked list
    // 11->11->11->13->13->20
    Node *head = (Node *)malloc(sizeof(Node));
    head->data = 11;
    head->next = (Node *)malloc(sizeof(Node));
    head->next->data = 11;
    head->next->next = (Node *)malloc(sizeof(Node));
    head->next->next->data = 11;
    head->next->next->next = (Node *)malloc(sizeof(Node));
    head->next->next->next->data = 13;
    head->next->next->next->next = (Node *)malloc(sizeof(Node));
    head->next->next->next->next->data = 13;
    head->next->next->next->next->next = (Node *)malloc(sizeof(Node));
    head->next->next->next->next->next->data = 20;
    head->next->next->next->next->next->next = NULL;

    // Printing old list
    Node *temp = head;
    while (temp != NULL)
    {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");

    removeDuplicates(head);

    // Printing new list
    temp = head;
    while (temp != NULL)
    {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");

    return 0;
}
Java
class Node {
    int data;
    Node next;

    Node(int new_data)
    {
        data = new_data;
        next = null;
    }
}

public class GFG {
    static Node removeDuplicates(Node head)
    {
        if (head == null)
            return head;

        if (head.next != null) {
            if (head.data == head.next.data) {
                Node temp = head.next;
                head.next = head.next.next;
                temp = null;

                return removeDuplicates(head);
            }
            else {
                removeDuplicates(head.next);
            }
        }

        return head;
    }

    public static void main(String[] args)
    {
        // Create a sorted linked list
        // 11->11->11->13->13->20
        Node head = new Node(11);
        head.next = new Node(11);
        head.next.next = new Node(11);
        head.next.next.next = new Node(13);
        head.next.next.next.next = new Node(13);
        head.next.next.next.next.next = new Node(20);

        // Printing old list
        Node temp = head;
        while (temp != null) {
            System.out.print(temp.data + " ");
            temp = temp.next;
        }
        System.out.println();

        removeDuplicates(head);

        // Printing new list
        temp = head;
        while (temp != null) {
            System.out.print(temp.data + " ");
            temp = temp.next;
        }
        System.out.println();
    }
}
Python
class Node:
    def __init__(self, new_data):
        self.data = new_data
        self.next = None


def removeDuplicates(head):
    if head is None:
        return head

    if head.next is not None:
        if head.data == head.next.data:
            temp = head.next
            head.next = head.next.next
            del temp

            return removeDuplicates(head)
        else:
            removeDuplicates(head.next)

    return head


def print_list(head):
    temp = head

    while temp is not None:
        print(temp.data, end=' ')
        temp = temp.next

    print()


if __name__ == "__main__":

    # Create a sorted linked list:
    # 11->11->11->13->13->20
    head = Node(11)
    head.next = Node(11)
    head.next.next = Node(11)
    head.next.next.next = Node(13)
    head.next.next.next.next = Node(13)
    head.next.next.next.next.next = Node(20)

    print_list(head)
    head = removeDuplicates(head)
    print_list(head)
C#
using System;

public class Node {
    public int data;
    public Node next;

    public Node(int new_data)
    {
        data = new_data;
        next = null;
    }
}

public class GFG {
    public static Node removeDuplicates(Node head)
    {
        if (head == null)
            return head;

        if (head.next != null) {
            if (head.data == head.next.data) {
                Node temp = head.next;
                head.next = head.next.next;
                temp = null;

                return removeDuplicates(head);
            }
            else {
                removeDuplicates(head.next);
            }
        }

        return head;
    }

    public static void Main()
    {
        // Create a sorted linked list
        // 11->11->11->13->13->20
        Node head = new Node(11);
        head.next = new Node(11);
        head.next.next = new Node(11);
        head.next.next.next = new Node(13);
        head.next.next.next.next = new Node(13);
        head.next.next.next.next.next = new Node(20);

        // Printing old list
        Node temp = head;
        while (temp != null) {
            Console.Write(temp.data + " ");
            temp = temp.next;
        }
        Console.WriteLine();

        removeDuplicates(head);

        // Printing new list
        temp = head;
        while (temp != null) {
            Console.Write(temp.data + " ");
            temp = temp.next;
        }
    }
}
JavaScript
class Node {
    constructor(new_data)
    {
        this.data = new_data;
        this.next = null;
    }
}

function removeDuplicates(head)
{
    if (head === null)
        return head;

    if (head.next !== null) {
        if (head.data === head.next.data) {
            let temp = head.next;
            head.next = head.next.next;
            temp = null;

            return removeDuplicates(head);
        }
        else {
            removeDuplicates(head.next);
        }
    }

    return head;
}

// Driver Code
// Create a sorted linked list
// 11->11->11->13->13->20
let head = new Node(11);
head.next = new Node(11);
head.next.next = new Node(11);
head.next.next.next = new Node(13);
head.next.next.next.next = new Node(13);
head.next.next.next.next.next = new Node(20);

// Printing old list
let temp = head;
while (temp !== null) {
    console.log(temp.data + " ");
    temp = temp.next;
}
console.log("\n");

removeDuplicates(head);

// Printing new list
temp = head;
while (temp !== null) {
    console.log(temp.data + " ");
    temp = temp.next;
}

Output
11 11 11 13 13 20 
11 13 20 
Comment