In this article, we will see program to remove duplicates from Linked list in Python.
Linked list with duplicates -
Linked list after removing duplicates -
Steps -
1. Traverse linkedlist, if a node is equal to next node (i.e duplicate node), link next of node to next next of node.
2. Repeat first step, until all the duplicated nodes are removed or node reaches to null.
Implementation -
def removeDuplicates(head):
while head and head.next:
if head.data==head.next.data:
head.next = head.next.next
else:
head=head.next
return head
Comments
Post a Comment