Python Program For Removing Duplicates From A Sorted Linked List
Last Updated :
15 Jun, 2022
Write a function that takes a list sorted in non-decreasing order and deletes any duplicate nodes from the list. The list should only be traversed once.
For example if the linked list is 11->11->11->21->43->43->60 then removeDuplicates() should convert the list to 11->21->43->60.
Algorithm:
Traverse the list from the head (or start) node. While traversing, compare each node with its next node. If the data of the next node is the same as the current node then delete the next node. Before we delete a node, we need to store the next pointer of the node
Implementation:
Functions other than removeDuplicates() are just to create a linked list and test removeDuplicates().
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def push( self , new_data):
new_node = Node(new_data)
new_node. next = self .head
self .head = new_node
def deleteNode( self , key):
temp = self .head
if (temp is not None ):
if (temp.data = = key):
self .head = temp. next
temp = None
return
while (temp is not None ):
if temp.data = = key:
break
prev = temp
temp = temp. next
if (temp = = None ):
return
prev. next = temp. next
temp = None
def printList( self ):
temp = self .head
while (temp):
print (temp.data , end = ' ' )
temp = temp. next
def removeDuplicates( self ):
temp = self .head
if temp is None :
return
while temp. next is not None :
if temp.data = = temp. next .data:
new = temp. next . next
temp. next = None
temp. next = new
else :
temp = temp. next
return self .head
llist = LinkedList()
llist.push( 20 )
llist.push( 13 )
llist.push( 13 )
llist.push( 11 )
llist.push( 11 )
llist.push( 11 )
print ( "Created Linked List: " )
llist.printList()
print ()
print ( "Linked List after removing" ,
"duplicate elements:" )
llist.removeDuplicates()
llist.printList()
|
Output:
Linked list before duplicate removal 11 11 11 13 13 20
Linked list after duplicate removal 11 13 20
Time Complexity: O(n) where n is the number of nodes in the given linked list.
Auxiliary Space: O(1)
Recursive Approach :
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def removeDuplicates(head):
if (head = = None ):
return
if (head. next ! = None ):
if (head.data = = head. next .data):
to_free = head. next
head. next = head. next . next
removeDuplicates(head)
else :
removeDuplicates(head. next )
return head
def push(head_ref, new_data):
new_node = Node(new_data)
new_node.data = new_data
new_node. next = head_ref
head_ref = new_node
return head_ref
def printList(node):
while (node ! = None ):
print (node.data, end = " " )
node = node. next
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 duplicate removal " ,
end = "")
printList(head)
removeDuplicates(head)
print ( "Linked list after duplicate removal " ,
end = "")
printList(head)
|
Output:
Linked list before duplicate removal 11 11 11 13 13 20
Linked list after duplicate removal 11 13 20
Time Complexity: O(n), where n is the number of nodes in the given linked list.
Auxiliary Space: O(n), due to recursive stack where n is the number of nodes in the given linked list.
Another Approach: Create a pointer that will point towards the first occurrence of every element and another pointer temp which will iterate to every element and when the value of the previous pointer is not equal to the temp pointer, we will set the pointer of the previous pointer to the first occurrence of another node.
Below is the implementation of the above approach:
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def removeDuplicates(head):
if (head = = None and
head. next = = None ):
return
current = head
while (current. next ):
if current.data = = current. next .data:
current. next = current. next . next
else :
current = current. next
return
def push(head_ref, new_data):
new_node = Node(new_data)
new_node.data = new_data
new_node. next = head_ref
head_ref = new_node
return head_ref
def printList(node):
while (node ! = None ):
print (node.data, end = " " )
node = node. next
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 ( "List before removal of "
"duplicates " , end = "")
printList(head)
removeDuplicates(head)
print ( "List after removal of "
"elements " , end = "")
printList(head)
|
Output:
List before removal of duplicates
11 11 11 13 13 20
List after removal of elements
11 13 20
Time Complexity: O(n) where n is the number of nodes in the given linked list.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Please refer complete article on Remove duplicates from a sorted linked list for more details!
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...