DSA · Linked List · #144
Remove Duplicates from Sorted List II
Module 42 · difficulty 3/5·⏱ 30:00starts on first keystroke
Given the `head` of a sorted linked list, delete **all** nodes that have duplicate numbers, leaving only nodes that appear exactly once in the original list. Return the head of the modified sorted linked list. Implement `deleteDuplicates(head)`. The list is represented as a chain of `{ val, next }` nodes; `head` may be `null`. Return the new head (also a `{ val, next }` chain or `null`).
Examples
head = [1,2,3,3,4,4,5]→[1,2,5]— Both 3s and both 4s are removed entirely.head = [1,1,1,2,3]→[2,3]— All three 1s are removed.head = [1,1]→[]— Every node was a duplicate, so the list becomes empty.
Constraints
- · The number of nodes in the list is in the range [0, 300].
- · -100 <= Node.val <= 100
- · The list is guaranteed to be sorted in ascending order.
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.