Skip to content

visual walkthrough

Remove Duplicates from Sorted List II

MediumDummy HeadReported at: AmazonMetaMicrosoft

Removing whole runs needs a dummy head and a careful look-ahead.

Solve on LeetCode

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

approachtimespace
Count, then rebuildO(n)O(n)
Skip whole runsO(n)O(1)

More walkthroughs