In this program we will see that how can we remove duplicate element from Linked List without sorting it
class Node:
def __init__(self,data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def display(self):
temp = self.head
while temp!=None:
print(temp.data,end="=>")
temp = temp.next
def length(self):
temp = self.head
count = 0
while temp!=None:
count = count+1
temp = temp.next
return count
def indexData(self,index):
temp = self.head
count =1
while count!=index:
temp = temp.next
count = count+1
return temp.data
def removeDuplicate(self):
temp = self.head
while temp!=None:
temp1=temp
while temp1.next!=None:
dup = temp1.next
if temp.data==temp1.next.data:
dup = temp1.next.next
temp1.next = dup
del dup
else:
temp1 = temp1.next
temp = temp.next
if __name__=="__main__":
llist = LinkedList()
first = Node(10)
second = Node(20)
third = Node(30)
fourth = Node(20)
fifth = Node(50)
llist.head = first
first.next = second
second.next = third
third.next = fourth
fourth.next = fifth
llist.display()
llist.removeDuplicate()
print("\nAfter removing duplicates")
llist.display()
No comments:
Post a Comment