AlgoMaster Logo

Remove Duplicates from Sorted List

easyFrequency5 min readUpdated June 23, 2026

Understanding the Problem

We have a linked list where nodes are already sorted in ascending order. Some nodes have duplicate values, and we need to remove the extra copies so that each value appears exactly once. The output should still be a sorted linked list.

Because the list is sorted, all duplicates are grouped together. To decide whether a node is a duplicate, we only compare it with the one right next to it. If they share the same value, the next node is a duplicate, and we remove it by adjusting pointers.

Removing an element from a linked list is a matter of relinking the next pointer. There is no shifting of later elements the way there would be in an array.

Key Constraints:

  • 0 <= number of nodes <= 300 → The list can be empty (0 nodes), so we handle a null head before dereferencing it.
  • Sorted in ascending order → This constraint drives the optimal approach. Duplicates are always consecutive, so comparing current.val with current.next.val is enough to detect every one of them.

Approach 1: Using a Hash Set

Intuition

Use a hash set to track which values we have already seen. Walk through the list, and for each node, check whether its next node's value is in the set. If it is, remove that node. If not, add the value to the set and keep the node.

This approach ignores the fact that the list is sorted, so it also works on an unsorted list. The cost is the extra memory for the set, which the sorted version avoids.

Algorithm

  1. If the list is empty, return null.
  2. Create a hash set and add the head node's value to it.
  3. Walk through the list, keeping track of the current node.
  4. For each node, if its next node's value is already in the set, remove the next node by setting current.next = current.next.next.
  5. If the next node's value is new, add it to the set and move current forward.
  6. Return the head.

Example Walkthrough

1Initialize: seen={1}, current=node(1)
1
current
1
2
3
3
null
1/6

Code

Because the list is sorted, duplicates are always adjacent, so the hash set is not needed. The next approach detects them with a single neighbor comparison and uses no extra space.

Approach 2: Single Pass In-Place (Optimal)

Intuition

In a sorted list, duplicates sit next to each other, so there is no need to remember every value we have encountered. Comparing each node with the one directly after it is enough.

Walk through the list with a single pointer. At each node, look at its next neighbor. If they share the same value, the neighbor is a duplicate, so we remove it by relinking the pointer. If they differ, we move forward.

Algorithm

  1. If the list is empty, return null.
  2. Start with a pointer current at the head.
  3. While current and current.next both exist:
    • If current.val == current.next.val, skip the next node by setting current.next = current.next.next.
    • Otherwise, move current forward to current.next.
  4. Return the head.

Example Walkthrough

1Initialize: current = node(1)
1
current
1
2
3
3
null
1/6

Code