Python Program For Removing Duplicates From An Unsorted Linked List
Last Updated :
13 Apr, 2023
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
class Node():
def __init__( self , data):
self .data = data
self . next = None
class LinkedList():
def __init__( self ):
self .head = None
def remove_duplicates( self ):
ptr1 = None
ptr2 = None
dup = None
ptr1 = self .head
while (ptr1 ! = None and
ptr1. next ! = None ):
ptr2 = ptr1
while (ptr2. next ! = None ):
if (ptr1.data = = ptr2. next .data):
dup = ptr2. next
ptr2. next = ptr2. next . next
else :
ptr2 = ptr2. next
ptr1 = ptr1. next
def printList( self ):
temp = self .head
while (temp ! = None ):
print (temp.data, end = " " )
temp = temp. next
print ()
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()
|
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)
Auxiliary Space: O(1)
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)
Auxiliary Space :O(1)
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
class Node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def printlist( self ):
temp = self .head
while (temp):
print (temp.data, end = " " )
temp = temp. next
def removeDuplicates( self , head):
if self .head is None or self .head. next is None :
return head
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
if __name__ = = "__main__" :
llist = LinkedList()
llist.head = Node( 10 )
second = Node( 12 )
third = Node( 11 )
fourth = Node( 11 )
fifth = Node( 12 )
sixth = Node( 11 )
seventh = Node( 10 )
llist.head. next = second
second. next = third
third. next = fourth
fourth. next = fifth
fifth. next = sixth
sixth. next = seventh
print ( "Linked List before removing Duplicates." )
llist.printlist()
llist.removeDuplicates(llist.head)
print ( "Linked List after removing duplicates." )
llist.printlist()
|
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), for using the HashSet.
Please refer complete article on Remove duplicates from an unsorted linked list for more details!
Share your thoughts in the comments
Please Login to comment...