visual walkthrough
Remove Duplicates from Sorted List II
Removing whole runs needs a dummy head and a careful look-ahead.
The idea
Sorted means copies sit together in a run. Any run longer than one node is removed completely, so you need the node before the run, not just the first copy.
Keep prev on the last node you're keeping and look ahead from it. A dummy head covers a run at the very start.
Complexity
| approach | time | space |
|---|---|---|
| Count, then rebuild | O(n) | O(n) |
| Skip whole runs | O(n) | O(1) |