Remove Duplicates From a Linked List

Given a sorted linked list, delete all duplicates such that each element appear only once.

Basic solution

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.

  1. While there is a next Node value present in the Linked List, Check if the current node's value is equal to the next node's value.
  2. If current node's value is equal to the next node's value, simply assign the next pointer of the current node to current's next's next - i.e. current.next.next.
  3. The above code will simply rearrange the current's next pointer to it's neighbour's next pointer, Essentially skipping the next element.

Time Complexity

  • O(n) time because all the elements inside of the list have to be traversed atleast once.
Solve Remove Duplicates From a Linked List on Algochurn

Practice all the solutions below

Practice the most popular front-end questions asked in coding interviews with   Frontend Churn