Skip to content
Related Articles

Related Articles

Python Program For Removing Duplicates From An Unsorted Linked List

Improve Article
Save Article
Like Article
  • Last Updated : 14 Dec, 2021

Write a removeDuplicates() function that takes a list and deletes any duplicate nodes from the list. The list is not sorted. 
For example if the linked list is 12->11->12->21->41->43->21 then removeDuplicates() should convert the list to 12->11->21->41->43.

METHOD 1 (Using two loops): 
This is the simple way where two loops are used. 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. 
Thanks to Gaurav Saxena for his help in writing this code.

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


Output: 

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

Time Complexity: O(n^2)

METHOD 2 (Use Sorting): 
In general, Merge Sort is the best-suited sorting algorithm for sorting linked lists efficiently. 
1) Sort the elements using Merge Sort. We will soon be writing a post about sorting a linked list. O(nLogn) 
2) Remove duplicates in linear time using the algorithm for removing duplicates in sorted Linked List. O(n) 
Please note that this method doesn’t preserve the original order of elements.
Time Complexity: O(nLogn)

METHOD 3 (Use Hashing): 
We traverse the link list from head to end. For every newly encountered element, we check whether it is in the hash table: if yes, we remove it; otherwise we put it in the hash table.

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("Linked List after removing duplicates.")
    llist.printlist()
# This code is contributed by rajataro0


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).

Please refer complete article on Remove duplicates from an unsorted linked list for more details!


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!