Given a sorted linked list, delete all duplicates such that each element appear only once.
The solution to this problem is quite straightforward.
SINCE THE LINKED LIST IS SORTED, we can take advantage of the fact that if there are duplicates, they'll be next to each other.
next
Node value present in the Linked List, Check if the current node's value is equal to
the next
node's value.next
pointer of the current
node to current's next's next
- i.e. current.next.next
.next
pointer to it's neighbour's next
pointer, Essentially skipping the next element.O(n)
time because all the elements inside of the list have to be traversed atleast once.