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.
- 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