Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Remove duplicates from an unsorted linked list

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Given an unsorted list of nodes. The task is to remove duplicates from the list. 

Examples:

Input: linked list = 12->11->12->21->41->43->21 
Output: 12->11->21->41->43. 
Explanation: Second occurrence o 12 and 21 is removed

Input: linked list = 12->11->12->21->41->43->21 
Output: 12->11->21->41->43. 

Naive Approach: 

Use two loops, Outer loop is used to pick the elements one by one and the Inner loop compares the picked element with the rest of the elements. 

Below is the Implementation of the above approach:

C++




/* C++ Program to remove duplicates in an unsorted
   linked list */
#include <bits/stdc++.h>
using namespace std;
 
/* A linked list node */
struct Node {
    int data;
    struct Node* next;
};
 
// Utility function to create a new Node
struct Node* newNode(int data)
{
    Node* temp = new Node;
    temp->data = data;
    temp->next = NULL;
    return temp;
}
 
/* Function to remove duplicates from a
   unsorted linked list */
void removeDuplicates(struct Node* start)
{
    struct Node *ptr1, *ptr2, *dup;
    ptr1 = start;
 
    /* Pick elements one by one */
    while (ptr1 != NULL && ptr1->next != NULL) {
        ptr2 = ptr1;
 
        /* Compare the picked element with rest
           of the elements */
        while (ptr2->next != NULL) {
            /* If duplicate then delete it */
            if (ptr1->data == ptr2->next->data) {
                /* sequence of steps is important here */
                dup = ptr2->next;
                ptr2->next = ptr2->next->next;
                delete (dup);
            }
            else /* This is tricky */
                ptr2 = ptr2->next;
        }
        ptr1 = ptr1->next;
    }
}
 
/* Function to print nodes in a given linked list */
void printList(struct Node* node)
{
    while (node != NULL) {
        printf("%d ", node->data);
        node = node->next;
    }
}
 
// Driver code
int main()
{
    /* The constructed linked list is:
     10->12->11->11->12->11->10*/
    struct Node* start = newNode(10);
    start->next = newNode(12);
    start->next->next = newNode(11);
    start->next->next->next = newNode(11);
    start->next->next->next->next = newNode(12);
    start->next->next->next->next->next = newNode(11);
    start->next->next->next->next->next->next = newNode(10);
 
    printf("Linked list before removing duplicates ");
    printList(start);
 
    removeDuplicates(start);
 
    printf("\nLinked list after removing duplicates ");
    printList(start);
    return 0;
}


C




/* C Program to remove duplicates in an unsorted
   linked list */
#include<stdio.h>
#include<stdlib.h>
 
/* A linked list node */
typedef struct Node {
    int data;
    struct Node* next;
} Node;
 
// Utility function to create a new Node
Node* newNode(int data)
{
    Node* temp = (Node*)malloc(sizeof(Node));
    temp->data = data;
    temp->next = NULL;
    return temp;
}
 
/* Function to remove duplicates from a
   unsorted linked list */
void removeDuplicates(Node* start)
{
    Node *ptr1, *ptr2, *dup;
    ptr1 = start;
 
    /* Pick elements one by one */
    while (ptr1 != NULL && ptr1->next != NULL) {
        ptr2 = ptr1;
 
        /* Compare the picked element with rest
           of the elements */
        while (ptr2->next != NULL) {
            /* If duplicate then delete it */
            if (ptr1->data == ptr2->next->data) {
                /* sequence of steps is important here */
                dup = ptr2->next;
                ptr2->next = ptr2->next->next;
                free(dup);
            }
            else /* This is tricky */
                ptr2 = ptr2->next;
        }
        ptr1 = ptr1->next;
    }
}
 
/* Function to print nodes in a given linked list */
void printList(struct Node* node)
{
    while (node != NULL) {
        printf("%d ", node->data);
        node = node->next;
    }
}
 
/* Driver program to test above function */
int main()
{
    /* The constructed linked list is:
     10->12->11->11->12->11->10*/
    struct Node* start = newNode(10);
    start->next = newNode(12);
    start->next->next = newNode(11);
    start->next->next->next = newNode(11);
    start->next->next->next->next = newNode(12);
    start->next->next->next->next->next = newNode(11);
    start->next->next->next->next->next->next = newNode(10);
 
    printf("Linked list before removing duplicates ");
    printList(start);
 
    removeDuplicates(start);
 
    printf("\nLinked list after removing duplicates ");
    printList(start);
 
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)


Java




// Java program to remove duplicates from unsorted
// linked list
 
class LinkedList {
 
    static Node head;
 
    static class Node {
 
        int data;
        Node next;
 
        Node(int d)
        {
            data = d;
            next = null;
        }
    }
 
    /* Function to remove duplicates from an
       unsorted linked list */
    void remove_duplicates()
    {
        Node ptr1 = null, ptr2 = null, dup = null;
        ptr1 = head;
 
        /* Pick elements one by one */
        while (ptr1 != null && ptr1.next != null) {
            ptr2 = ptr1;
 
            /* Compare the picked element with rest
                of the elements */
            while (ptr2.next != null) {
 
                /* If duplicate then delete it */
                if (ptr1.data == ptr2.next.data) {
 
                    /* sequence of steps is important here
                     */
                    ptr2.next = ptr2.next.next;
                    System.gc();
                }
                else /* This is tricky */ {
                    ptr2 = ptr2.next;
                }
            }
            ptr1 = ptr1.next;
        }
    }
 
    void printList(Node node)
    {
        while (node != null) {
            System.out.print(node.data + " ");
            node = node.next;
        }
    }
 
    public static void main(String[] args)
    {
        LinkedList list = new LinkedList();
        list.head = new Node(10);
        list.head.next = new Node(12);
        list.head.next.next = new Node(11);
        list.head.next.next.next = new Node(11);
        list.head.next.next.next.next = new Node(12);
        list.head.next.next.next.next.next = new Node(11);
        list.head.next.next.next.next.next.next
            = new Node(10);
 
        System.out.println(
            "Linked List before removing duplicates : ");
        list.printList(head);
 
        list.remove_duplicates();
        System.out.println("\n");
        System.out.println(
            "Linked List after removing duplicates : ");
        list.printList(head);
    }
}
// This code has been contributed by Mayank Jaiswal


Python3




# Python3 program to remove duplicates
# from unsorted linked list
 
 
class Node():
 
    def __init__(self, data):
 
        self.data = data
        self.next = None
 
 
class LinkedList():
 
    def __init__(self):
 
        # Head of list
        self.head = None
 
    def remove_duplicates(self):
 
        ptr1 = None
        ptr2 = None
        dup = None
        ptr1 = self.head
 
        # Pick elements one by one
        while (ptr1 != None and ptr1.next != None):
 
            ptr2 = ptr1
 
            # Compare the picked element with rest
            # of the elements
            while (ptr2.next != None):
 
                # If duplicate then delete it
                if (ptr1.data == ptr2.next.data):
 
                    # Sequence of steps is important here
                    dup = ptr2.next
                    ptr2.next = ptr2.next.next
                else:
                    ptr2 = ptr2.next
 
            ptr1 = ptr1.next
 
    # Function to print nodes in a
    # given linked list
    def printList(self):
        temp = self.head
 
        while(temp != None):
            print(temp.data, end=" ")
            temp = temp.next
 
        print()
 
 
# Driver code
list = LinkedList()
list.head = Node(10)
list.head.next = Node(12)
list.head.next.next = Node(11)
list.head.next.next.next = Node(11)
list.head.next.next.next.next = Node(12)
list.head.next.next.next.next.next = Node(11)
list.head.next.next.next.next.next.next = Node(10)
 
print("Linked List before removing duplicates :")
list.printList()
list.remove_duplicates()
print()
print("Linked List after removing duplicates :")
list.printList()
 
# This code is contributed by maheshwaripiyush9


C#




// C# program to remove duplicates from unsorted
// linked list
using System;
class List_ {
 
    Node head;
 
    class Node {
 
        public int data;
        public Node next;
 
        public
 
            Node(int d)
        {
            data = d;
            next = null;
        }
    }
 
    /* Function to remove duplicates from an
         unsorted linked list */
    void remove_duplicates()
    {
        Node ptr1 = null, ptr2 = null;
        ptr1 = head;
 
        /* Pick elements one by one */
        while (ptr1 != null && ptr1.next != null) {
            ptr2 = ptr1;
 
            /* Compare the picked element with rest
                      of the elements */
            while (ptr2.next != null) {
 
                /* If duplicate then delete it */
                if (ptr1.data == ptr2.next.data) {
 
                    /* sequence of steps is important here
                     */
                    ptr2.next = ptr2.next.next;
                }
                else /* This is tricky */
                {
                    ptr2 = ptr2.next;
                }
            }
            ptr1 = ptr1.next;
        }
    }
 
    void printList(Node node)
    {
        while (node != null) {
            Console.Write(node.data + " ");
            node = node.next;
        }
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        List_ list = new List_();
        list.head = new Node(10);
        list.head.next = new Node(12);
        list.head.next.next = new Node(11);
        list.head.next.next.next = new Node(11);
        list.head.next.next.next.next = new Node(12);
        list.head.next.next.next.next.next = new Node(11);
        list.head.next.next.next.next.next.next
            = new Node(10);
 
        Console.WriteLine(
            "Linked List_ before removing duplicates: ");
        list.printList(list.head);
 
        list.remove_duplicates();
        Console.WriteLine("");
        Console.WriteLine(
            "Linked List_ after removing duplicates: ");
        list.printList(list.head);
    }
}
 
// This code is contributed by gauravrajput1


Javascript




<script>
// javascript program to remove duplicates from unsorted
// linked list
 
 
 
    var head;
 
    class Node {
        constructor(val) {
            this.data = val;
            this.next = null;
        }
    }
    /*
     * Function to remove duplicates from an unsorted linked list
     */
    function remove_duplicates() {
        var ptr1 = null, ptr2 = null, dup = null;
        ptr1 = head;
 
        /* Pick elements one by one */
        while (ptr1 != null && ptr1.next != null) {
            ptr2 = ptr1;
 
            /*
             * Compare the picked element with rest of the elements
             */
            while (ptr2.next != null) {
 
                /* If duplicate then delete it */
                if (ptr1.data == ptr2.next.data) {
 
                    /* sequence of steps is important here */
                    dup = ptr2.next;
                    ptr2.next = ptr2.next.next;
     
                } else /* This is tricky */ {
                    ptr2 = ptr2.next;
                }
            }
            ptr1 = ptr1.next;
        }
    }
 
    function printList( node) {
        while (node != null) {
            document.write(node.data + " ");
            node = node.next;
        }
    }
 
     
        head = new Node(10);
        head.next = new Node(12);
        head.next.next = new Node(11);
        head.next.next.next = new Node(11);
        head.next.next.next.next = new Node(12);
        head.next.next.next.next.next = new Node(11);
        head.next.next.next.next.next.next = new Node(10);
 
        document.write("Linked List before removing duplicates : <br/> ");
        printList(head);
 
        remove_duplicates();
        document.write("<br/>");
        document.write("Linked List after removing duplicates : <br/> ");
        printList(head);
 
// This code contributed by aashish1995
</script>


Output

Linked list before removing duplicates 10 12 11 11 12 11 10 
Linked list after removing duplicates 10 12 11 

Time Complexity: O(N2)
Auxiliary Space: O(1)

Thanks to Gaurav Saxena for his help in writing this code.

Remove duplicates from an unsorted linked list using sorting:

Follow the below steps to Implement the idea:

Below is the implementation for above approach:

C++




#include <bits/stdc++.h>
using namespace std;
 
// structure of a node in the linked list
struct Node {
    int data;
    struct Node* next;
};
 
// function to insert a node in the linked list
void push(struct Node** head_ref, int new_data)
{
    struct Node* new_node = new Node;
    new_node->data = new_data;
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}
 
// function to sort the linked list
void sortList(struct Node* head)
{
    // pointer to traverse the linked list
    struct Node* current = head;
    struct Node* index = NULL;
 
    // loop to traverse the linked list
    while (current != NULL) {
 
        // loop to compare current node with all other nodes
        index = current->next;
        while (index != NULL) {
 
            // checking for duplicate values
            if (current->data == index->data) {
 
                // deleting the duplicate node
                current->next = index->next;
                free(index);
                index = current->next;
            }
            else {
                index = index->next;
            }
        }
        current = current->next;
    }
}
 
// function to display the linked list
void printList(struct Node* node)
{
    while (node != NULL) {
        cout << node->data << " ";
        node = node->next;
    }
    cout << endl;
}
 
// main function
int main()
{
    struct Node* head = NULL;
    push(&head, 20);
    push(&head, 13);
    push(&head, 13);
    push(&head, 11);
    push(&head, 11);
    push(&head, 11);
 
    cout << "Linked List before removing duplicates : \n";
    printList(head);
 
    sortList(head);
 
    cout << "Linked List after removing duplicates : \n";
    printList(head);
 
    return 0;
}
// This code is contributed by Veerendra Singh Rajpoot


Javascript




// structure of a node in the linked list
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
 
// function to insert a node in the linked list
function push(head_ref, new_data) {
const new_node = new Node(new_data);
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
 
// function to sort the linked list
function sortList(head) {
// pointer to traverse the linked list
let current = head;
let index = null;
 
// loop to traverse the linked list
while (current !== null) {
    // loop to compare current node with all other nodes
index = current.next;
while (index !== null) {
 
  // checking for duplicate values
  if (current.data === index.data) {
 
    // deleting the duplicate node
    current.next = index.next;
    index = current.next;
  }
  else {
    index = index.next;
  }
}
current = current.next;
}
}
 
// function to display the linked list
function printList(node) {
let str = "";
while (node !== null) {
str += node.data + " ";
node = node.next;
}
console.log(str);
}
 
// main function
let head = null;
head = push(head, 20);
head = push(head, 13);
head = push(head, 13);
head = push(head, 11);
head = push(head, 11);
head = push(head, 11);
 
console.log("Linked List before removing duplicates : ");
printList(head);
 
sortList(head);
 
console.log("Linked List after removing duplicates : ");
printList(head);


Java




import java.io.*;
 
// structure of a node in the linked list
class Node {
    int data;
    Node next;
 
    Node(int d)
    {
        data = d;
        next = null;
    }
}
 
// class for the linked list
class LinkedList {
    Node head;
 
    // function to insert a node in the linked list
    public void push(int new_data)
    {
        Node new_node = new Node(new_data);
        new_node.next = head;
        head = new_node;
    }
 
    // function to sort the linked list
    public void sortList()
    {
        // pointer to traverse the linked list
        Node current = head;
        Node index = null;
 
        // loop to traverse the linked list
        while (current != null) {
            // loop to compare current node with all other
            // nodes
            index = current.next;
            while (index != null) {
                // checking for duplicate values
                if (current.data == index.data) {
                    // deleting the duplicate node
                    current.next = index.next;
                    index = current.next;
                }
                else {
                    index = index.next;
                }
            }
            current = current.next;
        }
    }
 
    // function to display the linked list
    public void printList()
    {
        Node node = head;
        while (node != null) {
            System.out.print(node.data + " ");
            node = node.next;
        }
        System.out.println();
    }
}
 
// main class
class Main {
    public static void main(String[] args)
    {
        LinkedList ll = new LinkedList();
        ll.push(20);
        ll.push(13);
        ll.push(13);
        ll.push(11);
        ll.push(11);
        ll.push(11);
 
        System.out.println(
            "Linked List before removing duplicates : ");
        ll.printList();
 
        ll.sortList();
 
        System.out.println(
            "Linked List after removing duplicates : ");
        ll.printList();
    }
}


Python3




# Define the structure of a node in the linked list
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
 
# Function to insert a node in the linked list
 
 
def push(head_ref, new_data):
    new_node = Node(new_data)
    new_node.next = head_ref
    head_ref = new_node
    return head_ref
 
# Function to sort the linked list
 
 
def sortList(head):
    # Pointer to traverse the linked list
    current = head
    index = None
 
    # Loop to traverse the linked list
    while current is not None:
        # Loop to compare current node with all other nodes
        index = current.next
        while index is not None:
            # Checking for duplicate values
            if current.data == index.data:
                # Deleting the duplicate node
                current.next = index.next
                index = current.next
            else:
                index = index.next
        current = current.next
 
# Function to display the linked list
 
 
def printList(node):
    while node is not None:
        print(node.data, end=" ")
        node = node.next
    print()
 
 
# Main function
if __name__ == '__main__':
    head = None
    head = push(head, 20)
    head = push(head, 13)
    head = push(head, 13)
    head = push(head, 11)
    head = push(head, 11)
    head = push(head, 11)
 
    print("Linked List before removing duplicates :")
    printList(head)
 
    sortList(head)
 
    print("Linked List after removing duplicates :")
    printList(head)


C#




using System;
 
// structure of a node in the linked list
public class Node {
    public int data;
    public Node next;
 
    public Node(int d)
    {
        data = d;
        next = null;
    }
}
 
// class for the linked list
public class LinkedList {
    public Node head;
 
    // function to insert a node in the linked list
    public void push(int new_data)
    {
        Node new_node = new Node(new_data);
        new_node.next = head;
        head = new_node;
    }
 
    // function to sort the linked list
    public void sortList()
    {
        // pointer to traverse the linked list
        Node current = head;
        Node index = null;
 
        // loop to traverse the linked list
        while (current != null) {
            // loop to compare current node with all other
            // nodes
            index = current.next;
            while (index != null) {
                // checking for duplicate values
                if (current.data == index.data) {
                    // deleting the duplicate node
                    current.next = index.next;
                    index = current.next;
                }
                else {
                    index = index.next;
                }
            }
            current = current.next;
        }
    }
 
    // function to display the linked list
    public void printList()
    {
        Node node = head;
        while (node != null) {
            Console.Write(node.data + " ");
            node = node.next;
        }
        Console.WriteLine();
    }
}
 
// main class
public class GFG {
    static void Main(string[] args)
    {
        LinkedList ll = new LinkedList();
        ll.push(20);
        ll.push(13);
        ll.push(13);
        ll.push(11);
        ll.push(11);
        ll.push(11);
 
        Console.WriteLine(
            "Linked List before removing duplicates : ");
        ll.printList();
 
        ll.sortList();
 
        Console.WriteLine(
            "Linked List after removing duplicates : ");
        ll.printList();
    }
}


Output

Linked List before removing duplicates : 
11 11 11 13 13 20 
Linked List after removing duplicates : 
11 13 20 

Explanation:

push function inserts a new node at the beginning of the linked list.
sortList function sorts the linked list by comparing each node with all the other nodes and checking for duplicates. If a duplicate node is found, it is deleted.
printList function displays the linked list.
In the main function, nodes are added to the linked list using the push function and then the linked list is sorted using the sortList function. Finally, the linked list is displayed using the printList function

Note: that this method doesn’t preserve the original order of elements.

Time Complexity: O(N logN)
Auxiliary Space: O(1)

Remove duplicates from an unsorted linked list using Hashing:

Traverse the link list from head to end. For every newly encountered element, check whether if it is in the hash table: if yes, we remove it; otherwise put it in the hash table.

Follow the below steps to Implement the idea:

  • Create a Unordered set to keep a track of the visited elements.
  • Traverse the linked list from head to end node 
    • If current node is already present in the Hashset. Then delete the current node. 
    • Else move insert the node in the Hashset and move to the next node.
  • Return 

Below is the implementation of the above approach:

C++




/* C++ Program to remove duplicates in an unsorted
   linked list */
#include <bits/stdc++.h>
using namespace std;
 
/* A linked list node */
struct Node {
    int data;
    struct Node* next;
};
 
// Utility function to create a new Node
struct Node* newNode(int data)
{
    Node* temp = new Node;
    temp->data = data;
    temp->next = NULL;
    return temp;
}
 
/* Function to remove duplicates from a
   unsorted linked list */
void removeDuplicates(struct Node* start)
{
    // Hash to store seen values
    unordered_set<int> seen;
 
    /* Pick elements one by one */
    struct Node* curr = start;
    struct Node* prev = NULL;
    while (curr != NULL) {
        // If current value is seen before
        if (seen.find(curr->data) != seen.end()) {
            prev->next = curr->next;
            delete (curr);
        }
        else {
            seen.insert(curr->data);
            prev = curr;
        }
        curr = prev->next;
    }
}
 
/* Function to print nodes in a given linked list */
void printList(struct Node* node)
{
    while (node != NULL) {
        printf("%d ", node->data);
        node = node->next;
    }
}
 
/* Driver program to test above function */
int main()
{
    /* The constructed linked list is:
     10->12->11->11->12->11->10*/
    struct Node* start = newNode(10);
    start->next = newNode(12);
    start->next->next = newNode(11);
    start->next->next->next = newNode(11);
    start->next->next->next->next = newNode(12);
    start->next->next->next->next->next = newNode(11);
    start->next->next->next->next->next->next = newNode(10);
 
    printf("Linked list before removing duplicates : \n");
    printList(start);
 
    removeDuplicates(start);
 
    printf("\nLinked list after removing duplicates : \n");
    printList(start);
 
    return 0;
}


Java




// Java program to remove duplicates
// from unsorted linkedlist
 
import java.util.HashSet;
 
public class removeDuplicates {
    static class node {
        int val;
        node next;
 
        public node(int val) { this.val = val; }
    }
 
    /* Function to remove duplicates from a
       unsorted linked list */
    static void removeDuplicate(node head)
    {
        // Hash to store seen values
        HashSet<Integer> hs = new HashSet<>();
 
        /* Pick elements one by one */
        node current = head;
        node prev = null;
        while (current != null) {
            int curval = current.val;
 
            // If current value is seen before
            if (hs.contains(curval)) {
                prev.next = current.next;
            }
            else {
                hs.add(curval);
                prev = current;
            }
            current = current.next;
        }
    }
 
    /* Function to print nodes in a given linked list */
    static void printList(node head)
    {
        while (head != null) {
            System.out.print(head.val + " ");
            head = head.next;
        }
    }
 
    public static void main(String[] args)
    {
        /* The constructed linked list is:
         10->12->11->11->12->11->10*/
        node start = new node(10);
        start.next = new node(12);
        start.next.next = new node(11);
        start.next.next.next = new node(11);
        start.next.next.next.next = new node(12);
        start.next.next.next.next.next = new node(11);
        start.next.next.next.next.next.next = new node(10);
 
        System.out.println(
            "Linked list before removing duplicates :");
        printList(start);
 
        removeDuplicate(start);
 
        System.out.println(
            "\nLinked list after removing duplicates :");
        printList(start);
    }
}
 
// This code is contributed by Rishabh Mahrsee


Python3




# Python3 program to remove duplicates
# from unsorted linkedlist
 
 
class Node:
 
    def __init__(self, data):
 
        self.data = data
        self.next = None
 
 
class LinkedList:
 
    def __init__(self):
 
        self.head = None
 
    # Function to print nodes in a
    # given linked list
    def printlist(self):
 
        temp = self.head
 
        while (temp):
            print(temp.data, end=" ")
            temp = temp.next
 
    # Function to remove duplicates from a
    # unsorted linked list
    def removeDuplicates(self, head):
 
        # Base case of empty list or
        # list with only one element
        if self.head is None or self.head.next is None:
            return head
 
        # Hash to store seen values
        hash = set()
 
        current = head
        hash.add(self.head.data)
 
        while current.next is not None:
 
            if current.next.data in hash:
                current.next = current.next.next
            else:
                hash.add(current.next.data)
                current = current.next
 
        return head
 
 
# Driver code
if __name__ == "__main__":
 
    # Creating Empty list
    llist = LinkedList()
    llist.head = Node(10)
    second = Node(12)
    third = Node(11)
    fourth = Node(11)
    fifth = Node(12)
    sixth = Node(11)
    seventh = Node(10)
 
    # Connecting second and third
    llist.head.next = second
    second.next = third
    third.next = fourth
    fourth.next = fifth
    fifth.next = sixth
    sixth.next = seventh
 
    # Printing data
    print("Linked List before removing Duplicates.")
    llist.printlist()
    llist.removeDuplicates(llist.head)
    print("\nLinked List after removing duplicates.")
    llist.printlist()
 
# This code is contributed by rajataro0


C#




// C# program to remove duplicates
// from unsorted linkedlist
using System;
using System.Collections.Generic;
 
class removeDuplicates {
    class node {
        public int val;
        public node next;
 
        public node(int val) { this.val = val; }
    }
 
    // Function to remove duplicates from a
    // unsorted linked list
    static void removeDuplicate(node head)
    {
 
        // Hash to store seen values
        HashSet<int> hs = new HashSet<int>();
 
        // Pick elements one by one
        node current = head;
        node prev = null;
        while (current != null) {
            int curval = current.val;
 
            // If current value is seen before
            if (hs.Contains(curval)) {
                prev.next = current.next;
            }
            else {
                hs.Add(curval);
                prev = current;
            }
            current = current.next;
        }
    }
 
    // Function to print nodes in a
    // given linked list
    static void printList(node head)
    {
        while (head != null) {
            Console.Write(head.val + " ");
            head = head.next;
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
 
        // The constructed linked list is:
        // 10->12->11->11->12->11->10
        node start = new node(10);
        start.next = new node(12);
        start.next.next = new node(11);
        start.next.next.next = new node(11);
        start.next.next.next.next = new node(12);
        start.next.next.next.next.next = new node(11);
        start.next.next.next.next.next.next = new node(10);
 
        Console.WriteLine("Linked list before removing "
                          + "duplicates :");
        printList(start);
 
        removeDuplicate(start);
 
        Console.WriteLine("\nLinked list after removing "
                          + "duplicates :");
        printList(start);
    }
}
 
// This code is contributed by amal kumar choubey


Javascript




<script>
 
// JavaScript program to remove duplicates
// from unsorted linkedlist
 
     class node {
 
        constructor(val) {
            this.val = val;
            this.next = null;
        }
    }
 
    /*
     Function to remove duplicates
     from a unsorted linked list
     */
    function removeDuplicate( head) {
        // Hash to store seen values
        var hs =  new Set();
 
        /* Pick elements one by one */
        var current = head;
        var prev = null;
        while (current != null) {
            var curval = current.val;
 
            // If current value is seen before
            if (hs.has(curval)) {
                prev.next = current.next;
            } else {
                hs.add(curval);
                prev = current;
            }
            current = current.next;
        }
 
    }
 
    /* Function to print nodes in a
    given linked list */
    function printList( head) {
        while (head != null) {
            document.write(head.val + " ");
            head = head.next;
        }
    }
 
     
        /*
         * The constructed linked list is:
         10->12->11->11->12->11->10
         */
         start = new node(10);
        start.next = new node(12);
        start.next.next = new node(11);
        start.next.next.next = new node(11);
        start.next.next.next.next = new node(12);
        start.next.next.next.next.next = new node(11);
        start.next.next.next.next.next.next = new node(10);
 
        document.write(
        "Linked list before removing duplicates :<br/>"
        );
        printList(start);
 
        removeDuplicate(start);
 
        document.write(
        "<br/>Linked list after removing duplicates :<br/>"
        );
        printList(start);
 
// This code is contributed by todaysgaurav
 
</script>


Output

Linked list before removing duplicates : 
10 12 11 11 12 11 10 
Linked list after removing duplicates : 
10 12 11 

Thanks to bearwang for suggesting this method.

Time Complexity: O(N), on average (assuming that hash table access time is O(1) on average).  
Auxiliary Space : O(N), As extra space is used to store the elements in the stack.


My Personal Notes arrow_drop_up
Last Updated : 03 Apr, 2023
Like Article
Save Article
Similar Reads
Related Tutorials