Data Structures19 sections · 729 units
Open in Course

Dummy Head Technique

Simplify edge cases

Many linked list operations have awkward edge cases when modifying the head. The dummy head technique eliminates them.

Create a dummy node that points to the real head: ```pseudocode dummy = new ListNode(0) dummy.next = head


You never need special logic for "what if I'm modifying the head?" After your algorithm finishes, return `dummy.next` as the new head. 

The dummy node exists only during processing. You don't include it in the result. This technique is especially useful for deletion operations and merging lists. 

I'll use it throughout this section. Time: $O(n)$. Space: $O(1)$.