Remove duplicates from sorted list II
viaLeetCode
Problem Given the head of a sorted linked list, remove ALL nodes whose value appears more than once, keeping only values that were already distinct.
Input / Output
- Input: head of sorted list.
- Output: head of the filtered list.
Constraints
- Up to 300 nodes classically; O(n) time, O(1) space, single pass preferred.
Example
- 1→2→3→3→4→4→5 → 1→2→5 (contrast with variant I, which keeps one copy: 1→2→3→4→5).
Expected approach
- Dummy head + prev pointer: if the node after prev starts a duplicate run (equal consecutive values), skip the whole run and splice prev.next past it WITHOUT advancing prev (the next candidate may also start a run); otherwise advance prev. prev only advances after a confirmed-distinct node — that's the subtlety. O(n)/O(1).
asked …