Skip to content

visual walkthrough

Remove Linked List Elements

EasyDummy HeadReported at: AmazonMicrosoftMeta+1

Shows why a dummy head removes head-deletion special cases.

Solve on LeetCode

The idea

To remove a node you need the node before it, but the head has none. That's why removing from the front usually needs extra code.

A dummy node in front of the head gives every real node a predecessor, so one loop handles the head and the rest alike.

Complexity

approachtimespace
Head first, then the restO(n)O(1)
Dummy headO(n)O(1)

More walkthroughs