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: dummy = new ListNode(0) dummy.next = head Now every real node has a predecessor.
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: . Space: .