Eliminate duplicates from LL
You have been given a singly linked list of integers where the elements are sorted in ascending order. Write a function that removes the consecutive duplicate values such that the given list only contains unique elements and returns the head to the updated list.
Input format :
Remember/Consider :
Output format :
Constraints :
Sample Input 1 :
Sample Output 1 :
Sample Input 2 :
Sample Output 2 :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node: | |
def __init__(self,data): | |
self.data=data | |
self.next=None | |
def LinkedList(arr): | |
head=None | |
tail=None | |
if len(arr)<1: | |
return | |
else: | |
for i in arr: | |
if i ==-1: | |
break | |
NewNode=Node(i) | |
if head is None: | |
head=NewNode | |
tail=NewNode | |
else: | |
tail.next=NewNode | |
tail=NewNode | |
return head | |
def printLL(head): | |
while head is not None: | |
print(head.data,end=" ") | |
head=head.next | |
# This function removes duplicates from list | |
def removeDuplicates(head): | |
temp = 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 head | |
t=int(input()) | |
for i in range(t): | |
arr=list(map(int,input().split())) | |
printLL(removeDuplicates(LinkedList(arr))) |
Comments
Post a Comment
Please give us your valuable feedback