Data Structures & Algorithms (DSA) Interview Mastery · Chapter 4 · ~15 min · Intermediate
Linked Lists & In-Place Reversal
Master the art of pointer manipulation without allocating extra memory: reverse singly linked lists with 3 pointers, prevent lost node bridges, and detect infinite loops using Floyd's cycle runner.
- Master pointer mutation and the 3-pointer reversal dance
- Detect cycles and find loop starts using Floyd's runner algorithm
- Reverse sublists and linked lists in O(1) auxiliary space
- Prevent common pitfalls like lost next pointers and null pointer dereferences
Linked Lists & In-Place Reversal: learn it visually
Master the art of pointer manipulation without allocating extra memory: reverse singly linked lists with 3 pointers, prevent lost node bridges, and detect infinite loops using Floyd's cycle runner.
1Everyday Intuition: The Scavenger Hunt Clue Chain
Unlike an array where all elements sit in adjacent physical lockers (memory addresses 100, 104, 108), a Linked List is like a scavenger hunt across an entire city:
- Clue #1 is at the coffee shop, and contains a note: "Your next clue is taped under the park bench."
- Clue #2 is at the park bench, and contains a note: "Your next clue is behind the library clock."
If somebody steals Clue #2, the rest of the hunt is severed forever! You cannot jump directly to Clue #3 because nobody knows where it is located.
This is why linked list algorithms require extreme care: when you redirect a pointer, you must always preserve a reference to where the old pointer was pointing before cutting the wire.
Why does accessing the k-th element of a singly linked list take O(k) time instead of O(1)?
2Architecture & Code Mechanics: In-Place Reversal
To reverse a linked list in $O(1)$ auxiliary space, we maintain three pointers: prev, curr, and next_temp.
Wrong idea: Allocating a new list or copying values to an array (O(N) space)
Copying all node values into a standard Python list, reversing the array, and creating new nodes wastes O(N) extra memory heap allocation.
Real system: In-place pointer reversal using 3 references (O(1) space)
By tracking previous, current, and next temporary pointers, links are flipped one by one in O(1) auxiliary space.
The Canonical 4-Step Pointer Reversal Dance:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def reverse_list(head: ListNode | None) -> ListNode | None:
prev = None
curr = head
while curr:
next_temp = curr.next # Step 1: Secure forward bridge
curr.next = prev # Step 2: Reverse current link
prev = curr # Step 3: Advance prev to current
curr = next_temp # Step 4: Advance curr to next
return prev # prev is now pointing to the new head!When the 'while curr:' loop finishes in reverse_list, why do we return prev instead of curr?
3Interactive Visual Simulation: 3-Pointer Reversal Walk
Step through the in-place pointer reversal one move at a time. Watch how prev and curr advance while links flip backward:
Live 3-Pointer Linked List In-Place Reversal
Initial State: prev = None, curr = Node(10). Ready to begin in-place reversal!
In Floyd's cycle detection algorithm, if a cycle of length C exists, why must the fast runner (advancing 2 steps) collide with the slow runner (advancing 1 step)?
4Hands-On Challenge: Fix the Reversal Severing Bug
A junior developer tried to reverse a linked list in-place, but accidentally severed the chain before saving the next node reference, causing an infinite loop or missing nodes.
Fix the implementation so all three checks pass, then press Run & verify:
🎯 Find and fix the bugs in the code above, then press Run & verify to test your solution.
Need a hint?
- Save
next_temp = curr.nextbefore assigningcurr.next = prev. - Advance
prev = curr, then advancecurr = next_temp. - Return
prevafter the while loop finishes.
5Architecture Recap & Next Steps
- O(1) Space In-Place: Reversing links by reassigning pointers uses zero additional memory allocations.
- Preserve Before Severing: Always store
next_temp = curr.nextbefore mutatingcurr.next. - Return Prev: When
currbecomesNone,prevholds the new head node. - Floyd's Runner: Fast & slow pointers detect cycles in $O(N)$ time and $O(1)$ memory without a hash set.
Ready for Stacks, Queues & Monotonic Stack Tricks? Mark this chapter complete: