Magicsheet logo

Remove Duplicates from Sorted List

Easy
48.3%
Updated 6/1/2025

Remove Duplicates from Sorted List

What is this problem about?

The Remove Duplicates from Sorted List interview question asks you to traverse a sorted singly linked list and delete all nodes that contain duplicate values, keeping only one occurrence of each value. Because the list is already sorted, all duplicates are guaranteed to be adjacent, making this a clean and elegant application of basic linked list pointer manipulation.

Why is this asked in interviews?

This problem is asked at Apple, Microsoft, Meta, Amazon, Google, and Bloomberg as an entry-level linked list question. It evaluates whether a candidate understands pointer traversal, the concept of in-place node deletion, and edge case handling (empty list, all duplicates). It is a foundational exercise that builds the intuition needed for more complex linked list problems like reversals and merges.

Algorithmic pattern used

The pattern is single-pass linked list traversal with direct pointer manipulation. Use a single pointer curr starting at the head. At each step, compare curr.val with curr.next.val. If they match, skip curr.next by setting curr.next = curr.next.next. If they don't match, advance curr to curr.next. Continue until curr or curr.next is null.

Example explanation

List: 1 → 1 → 2 → 3 → 3

  • curr=1: next=1 (duplicate) → set curr.next = curr.next.next (the 2). List: 1 → 2 → 3 → 3.
  • curr=1: next=2 (different) → advance. curr=2.
  • curr=2: next=3 (different) → advance. curr=3.
  • curr=3: next=3 (duplicate) → set curr.next = null. List: 1 → 2 → 3.
  • curr=3: next=null → done.

Result: 1 → 2 → 3.

Common mistakes candidates make

  • Advancing curr when a duplicate is found instead of staying at the current node to handle consecutive duplicates (e.g., 1 → 1 → 1).
  • Forgetting to handle an empty list or a single-node list.
  • Attempting to use a set for deduplication — unnecessary given the sorted property.
  • Not checking curr.next before accessing curr.next.val, causing null pointer errors.

Interview preparation tip

For the Remove Duplicates from Sorted List coding problem, the linked list interview pattern is straightforward once you internalize the key rule: only advance your pointer when the current node is confirmed unique. Practice tracing through a list with three or more consecutive duplicates to verify your loop handles them all. Interviewers at Revolut and Nvidia use this as a warm-up to lead into Remove Duplicates from Sorted List II, so be ready for the follow-up.

Similar Questions