Python Day 24: More Linked Lists




A Node class is provided for you in the editor. A Node object has an integer data field, data, and a Node instance pointer, next, pointing to another node (i.e.: the next node in a list).

A removeDuplicates function is declared in your editor, which takes a pointer to the head node of a linked list as a parameter. Complete removeDuplicates so that it deletes any duplicate nodes from the list and returns the head of the updated list.

Note: The head+ pointer may be null, indicating that the list is empty. Be sure to reset your next pointer when performing deletions to avoid breaking the list.



  1. class Node:
  2. def __init__(self,data):
  3. self.data = data
  4. self.next = None
  5. class Solution:
  6. def insert(self,head,data):
  7. p = Node(data)
  8. if head==None:
  9. head=p
  10. elif head.next==None:
  11. head.next=p
  12. else:
  13. start=head
  14. while(start.next!=None):
  15. start=start.next
  16. start.next=p
  17. return head
  18. def display(self,head):
  19. current = head
  20. while current:
  21. print(current.data,end=' ')
  22. current = current.next
  23. def removeDuplicates(self,head):
  24. #Write your code here
  25. node = head
  26. while node:
  27. if node.next:
  28. if node.data == node.next.data:
  29. node.next = node.next.next
  30. continue
  31. node = node.next
  32. return head
  33. mylist= Solution()
  34. T=int(input())
  35. head=None
  36. for i in range(T):
  37. data=int(input())
  38. head=mylist.insert(head,data)
  39. head=mylist.removeDuplicates(head)
  40. mylist.display(head);
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]


donate a cup of tea :)


Join Our Facebook Group

Share this solution






codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube