跪拜 Guibai
← All articles
Algorithms

The Dummy Node Trick That Makes Merging Sorted Linked Lists Trivial

By 小月土星 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

The dummy-node pattern generalizes to nearly every linked-list insertion and deletion problem. Recognizing that the remaining chain can be attached whole — rather than node-by-node — cuts out unnecessary loops and signals fluency with pointer-based data structures.

Summary

Merging two ascending linked lists trips up beginners because the head node needs special treatment and the two lists rarely end at the same time. A dummy node sidesteps the head problem entirely: every new node attaches to a working cursor that starts at the dummy, so the real head is always `dummy.next`. The core loop compares the current nodes of both lists, links the smaller one, and advances the corresponding pointer. When one list runs out, the entire remaining chain can be attached in a single assignment because linked-list nodes already carry their successors. The result is an O(m+n) time, O(1) space solution that interviewers expect. The post walks through a verbose, beginner-friendly implementation with explicit remaining-node loops, then contrasts it with the one-line `cur.next = list1 || list2` finish that belongs in a tight interview answer.

Takeaways
Place a dummy ListNode before the real head so every merge step uses the same `cur.next = …` logic, removing the need to initialize the result head separately.
Advance the working cursor `cur` after every attachment so it always points to the last node of the merged list.
When one input list is exhausted, attach the surviving list with a single assignment (`cur.next = list1 || list2`) instead of a loop; the rest of the chain follows automatically.
The `cur = cur.next` step can be hoisted out of the if-else branches to appear once per iteration.
Time complexity is O(m+n) because each node is visited exactly once; space complexity is O(1) beyond the input nodes.
Conclusions

The post’s beginner code uses two explicit while-loops to drain the remaining list, which is correct but redundant. That verbosity is pedagogically useful for learning pointer mechanics, yet the jump to a one-line tail attachment is what separates a working solution from an interview-ready one.

Many linked-list problems that feel edge-case-heavy — reversing sublists, removing duplicates, partitioning — collapse into uniform logic once a dummy node is introduced. The pattern is worth internalizing as a reflex.

The explanation that `cur.next = list1 || list2` works because a node’s `next` pointer already owns the rest of the chain is a small but crucial insight that often gets skipped in algorithm tutorials.

Concepts & terms
Dummy node
A placeholder ListNode with no meaningful value, created before the real head of a linked list. It allows every insertion to follow the same pointer logic, avoiding special-case code for the first node.
Working cursor (cur pointer)
A reference that always points to the last node of a linked list being built. New nodes are attached to `cur.next`, and `cur` is then advanced to the newly attached node.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗