Remove Duplicates


Given the head node of a singly linked list, remove all duplicate elements.


To remove the duplicates, we will need to iterate through the entire linked list and keep track of the elements we come across. If we have already encountered an element before, we then remove it from the linked list.


def remove_duplicates(head):
curr = head
prev = None
duplicates = {}
while curr:
if in duplicates:
# remove node =
curr = None
duplicates[] = 1
prev = curr
curr =


  • Time complexity: O(n) where n is the length of linked list
  • Space complexity: O(n) where n is number of elements in linked list