62. Remove Duplicates from Sorted List II
mediumAsked at VercelRemove ALL nodes that have duplicates from a sorted linked list (keep only unique values). Vercel asks this for the two-cursor + skip-runs pattern with a dummy head — same shape as their event-stream dedup that drops any event seen more than once.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Vercel loops.
- Glassdoor (2025-Q4)— Vercel platform onsite; dummy head + run-skipping expected.
- Blind (2026-Q1)— Listed in Vercel screen pool.
Problem
Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.
Constraints
The number of nodes in the list is in the range [0, 300].-100 <= Node.val <= 100The list is guaranteed to be sorted in ascending order.
Examples
Example 1
head = [1,2,3,3,4,4,5][1,2,5]Example 2
head = [1,1,1,2,3][2,3]Approaches
1. Two-pass with counter
Pass 1: count each value. Pass 2: keep only those with count == 1.
- Time
- O(n)
- Space
- O(n)
// Allocates a Map; works but doesn't use the sortedness.Tradeoff: Wastes the sort property.
2. Dummy head + run-skipping (optimal)
Dummy.next = head. Cursor prev = dummy. Walk: if current and current.next have same value, skip the whole run. Else advance prev.
- Time
- O(n)
- Space
- O(1)
function deleteDuplicates(head) {
const dummy = { val: 0, next: head };
let prev = dummy;
while (prev.next) {
let cur = prev.next;
if (cur.next && cur.val === cur.next.val) {
const v = cur.val;
while (cur && cur.val === v) cur = cur.next;
prev.next = cur;
} else {
prev = prev.next;
}
}
return dummy.next;
}Tradeoff: Single pass. Dummy head handles the case where the head itself is a duplicate. The inner while skips the entire run of equal values.
Vercel-specific tips
Vercel grades the dummy head + run-skipping. Bonus signal: the inner while loop that skips ALL nodes equal to the duplicate value (not just one), and the correct prev-advance ONLY when current is unique (else prev stays put because a new run might start).
Common mistakes
- Advancing prev unconditionally — leaves duplicate nodes in the output.
- Skipping only one duplicate, not the whole run — leaves the second copy.
- Forgetting the dummy — head duplicates become a special case.
Follow-up questions
An interviewer at Vercel may pivot to one of these next:
- Remove Duplicates from Sorted List (LC 83) — keep one copy of each.
- Unsorted variant — needs a Set.
- Remove triplicates only (keep singletons and duplicates).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why a dummy head?
If the head itself is a duplicate, you need somewhere for 'prev' to point. The dummy provides that anchor.
Why does prev only advance when current is unique?
When we skip a duplicate run, prev.next now points to the post-run node. We don't advance prev because we need to check if the new prev.next is itself the start of another duplicate run.
Practice these live with InterviewChamp.AI
Drill Remove Duplicates from Sorted List II and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →