跪拜 Guibai
← All articles
Algorithms · JavaScript · Data Structures

Every Linked List Operation Is Just `cur.next = ...` — Here's How to Derive, Not Memorize

By 烬羽 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Linked list questions remain a staple of technical interviews, yet most candidates memorize operations as isolated recipes and trip over edge cases under pressure. Reducing every operation to a `next` reassignment — and defaulting to a dummy node — turns fragile memorization into a derivable skill that holds up when the problem is unfamiliar.

Summary

Linked list operations collapse into one idea: changing what a node's `next` points to. Traversal moves your gaze (`cur = cur.next`), deletion skips a node (`cur.next = cur.next.next`), insertion connects the new node before breaking the old chain, and reversal flips each node's `next` to point backward. A dummy head node gives every real node a predecessor, eliminating special-case branches for the head.

Fast-slow pointers solve cycle detection and Kth-from-end problems with the same chase logic. Two-pointer path-swapping handles intersection, and a dummy plus comparison pointer merges sorted lists. The underlying pattern is always the same: identify which `next` to rewrite, then move the right pointers.

The practical takeaway is a two-step habit — draw three nodes with their `next` arrows, then write the single `xxx.next = yyy` line that does the work. Common bugs like advancing after deletion or returning `head` instead of `dummy.next` become obvious once the pointer model is clear.

Takeaways
All four core linked list operations — traversal, deletion, insertion, reversal — are just different assignments to a node's `next` pointer.
Deletion is `cur.next = cur.next.next`; do not advance `cur` afterward, because the new `cur.next` still needs checking.
Insertion must connect the new node first (`newNode.next = cur.next`), then break the old link (`cur.next = newNode`), or the rest of the list is lost.
Reversal uses three pointers: save the next node, point current back to previous, then advance both pre and cur.
A dummy head node gives every real node a predecessor, eliminating special-case logic for the head and making deletion safe.
Fast-slow pointers detect cycles (fast laps slow) and find the Kth-from-end node (fast leads by K steps).
Intersection of two lists is solved by swapping paths so both pointers travel the same total distance.
Merging sorted lists uses a dummy node and a tail pointer that always attaches the smaller head and advances.
Always return `dummy.next`, not `head`, because the original head may have been deleted or moved.
Before writing code, draw three nodes with their `next` arrows and identify the single `xxx.next = yyy` line that performs the operation.
Conclusions

The pedagogical shift from 'four operations' to 'one pointer reassignment with four shapes' mirrors how expert developers actually reason about linked structures — not as a catalog of recipes but as a tiny state machine where only `next` changes.

Most linked list bugs (advancing after deletion, reversing insert order, returning the wrong head) are symptoms of treating operations as opaque steps rather than visualizing which pointer moves where. The dummy node alone eliminates an entire class of head-special-case errors that interviewers routinely see.

The fast-slow pointer mnemonic — chase, lead, swap, compare — covers the majority of medium and hard linked list problems, yet many candidates still approach each problem as a fresh puzzle instead of recognizing the underlying pattern.

Concepts & terms
Dummy head node
A synthetic node inserted before the real head of a linked list. It ensures every node (including the original head) has a predecessor, which unifies deletion and insertion logic and avoids special-case branches for the first node.
Floyd's cycle detection (fast-slow pointers)
An algorithm that uses two pointers moving at different speeds (one step vs. two steps) to detect cycles in a linked list. If they meet, a cycle exists; resetting one pointer to the head and moving both at the same speed finds the cycle entry point.
Connect-first-then-break (insertion order)
The mandatory sequence for inserting a node into a linked list: the new node's `next` must be set to the current node's `next` before the current node's `next` is redirected to the new node. Reversing the order severs the link to the rest of the list.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗