Linked List Manipulation
01 / The one-sentence essence
To rewrite a linked list in place, walk it with three pointers prev, curr, next — save before you flip, advance after you flip, and the rest of the list stays reachable through every step.
Problemreverse a singly linked list in placeInput[1 → 2 → 3 → 4 → 5]
Three pointers: prev at null, curr at the head. next will hold the saved pointer at each step.
step
0 / 16
prev
null
curr
1 [0]
action
init
0 / 16
02 / The pattern signature
# reverse a singly linked listprev ← nullcurr ← headwhile curr ≠ null:next ← curr.next // 1. savecurr.next ← prev // 2. flipprev ← curr // 3. advance prevcurr ← next // 4. advance currreturn prev // new head
03 / When to recognize this pattern
"in place / O(1) space"
The constraint forbids building a second list — you must rewire the existing nodes. That's where the multi-pointer dance earns its keep.
"reverse / reorder"
You're changing the structure of the list, not the values. The values stay; the
.next arrows shift."sublist / range"
Reverse a chunk between two positions, not the whole list. Same algorithm, plus stitching at the boundaries.
"merge / interleave"
Two lists into one, alternating or sorted. A "dummy head" trick simplifies the edge-of-list bookkeeping — you splice from
dummy.next at the end.04 / Common pitfalls
Losing the rest of the list before saving next.
If you flip
curr.next ← prev before saving the original curr.next, you've cut the tail loose. The save step is non-negotiable — it's the whole reason there are three pointers.Wrong return value.
After the loop ends,
curr is null and prev is the new head. Returning curr would give null on a non-empty list — a common slip.Not handling the empty list.
The loop doesn't execute on
head = null, and prev stays null. That's correct — but only because the loop body never runs. Verify the empty case explicitly; it's easy to introduce a null-dereference if you peek curr.next before the guard.05 / Go practice — on LeetCode
easy9
01Merge Two Sorted Lists— LC 21→02Remove Duplicates from Sorted List— LC 83→03Linked List Cycle— LC 141→04Intersection of Two Linked Lists— LC 160→05Remove Linked List Elements— LC 203→06Reverse Linked List— LC 206→07Palindrome Linked List— LC 234→08Middle of the Linked List— LC 876→09Convert Binary Number in a Linked List to Integer— LC 1290→
medium33
01Add Two Numbers— LC 2→02Remove Nth Node From End of List— LC 19→03Swap Nodes in Pairs— LC 24→04Rotate List— LC 61→05Remove Duplicates from Sorted List II— LC 82→06Partition List— LC 86→07Reverse Linked List II— LC 92→08Convert Sorted List to Binary Search Tree— LC 109→09Flatten Binary Tree to Linked List— LC 114→10Populating Next Right Pointers in Each Node— LC 116→11Populating Next Right Pointers in Each Node II— LC 117→12Copy List with Random Pointer— LC 138→13Linked List Cycle II— LC 142→14Reorder List— LC 143→15LRU Cache— LC 146→16Sort List— LC 148→17Delete Node in a Linked List— LC 237→18Odd Even Linked List— LC 328→19Plus One Linked List— LC 369→20Linked List Random Node— LC 382→21Flatten a Multilevel Doubly Linked List— LC 430→22Add Two Numbers II— LC 445→23Design Linked List— LC 707→24Split Linked List in Parts— LC 725→25Next Greater Node In Linked List— LC 1019→26Remove Zero Sum Consecutive Nodes from Linked List— LC 1171→27Print Immutable Linked List in Reverse— LC 1265→28Linked List in Binary Tree— LC 1367→29Merge In Between Linked Lists— LC 1669→30Swapping Nodes in a Linked List— LC 1721→31Reverse Nodes in Even Length Groups— LC 2074→32Delete the Middle Node of a Linked List— LC 2095→33Maximum Twin Sum of a Linked List— LC 2130→