Micro-questions over the linked list reversal pattern. Answer in a full sentence before opening the toggle. Format explained in the arrays bank.


A. Why it exists

1. Give the stack-of-papers analogy for both approaches.

Answer

Building a new stack by moving papers one at a time is time and space. Rewiring each paper’s connection in place is time and space.

2. State the pattern.

Answer

Reverse a linked list by rewiring .next pointers as you walk through it, instead of building a new list.

3. What is pointer rewiring?

Answer

Changing the .next references to point to different nodes.


B. The mechanism

4. Which three pointers do you keep, and why each?

Answer

prev — the node before the current one, which is where curr.next must point. curr — where you are. nxt — a temporary hold on curr.next, because you would otherwise lose your only path forward the moment you rewire it.

5. Write the loop.

Answer
prev = None
curr = head
while curr:
    nxt = curr.next      # save before overwriting
    curr.next = prev     # reverse the pointer
    prev = curr
    curr = nxt
return prev              # prev is the new head

6. Why is prev initialised to None?

Answer

The old head becomes the new tail, and a tail’s next must be None. It acts as the sentinel.

7. Why return prev and not curr?

Answer

When the loop ends, curr is None and prev is the last node processed — which is the new head.

8. Trace 1→2→3→4→5.

Answer

After step 1: 1 ← 2, rest untouched, prev=1, curr=2. After step 2: 1 ← 2 ← 3, prev=2, curr=3. Finally None ← 1 ← 2 ← 3 ← 4 ← 5 with prev = 5.


C. Reversing a sublist

9. Describe the three phases.

Answer
  1. Walk to the start of the range, keeping a reference to the node just before it.
  2. Reverse only within the range, using the same three-pointer loop.
  3. Stitch the reversed section back — three pieces (before, reversed middle, after) glued together.

10. What is the most common bug here?

Answer

Off-by-one on where the sublist starts and ends. Draw the before/after picture first.

11. What if the sublist starts at position 1?

Answer

There is no node before it, so the head itself changes. A sentinel/dummy node before the head removes the special case entirely — you reattach to dummy.next and return that.


D. Recursive versus iterative

12. Write the recursive version.

Answer
if not head or not head.next:
    return head
new_head = reverse_list_recursive(head.next)
head.next.next = head
head.next = None
return new_head

13. What does head.next.next = head do?

Answer

Makes the next node point back at the current one — the reversal itself, done on the way back up the recursion.

14. Why head.next = None immediately after?

Answer

Otherwise the two nodes point at each other, creating a two-node cycle.

15. Give the trade-offs.

Answer

Iterative: space, no recursion overhead, but more complex pointer management. Recursive: simpler to understand, but stack space and risk of stack overflow on long lists.


E. Traps

16. What is “the one line that makes or breaks this algorithm”?

Answer

nxt = curr.next before curr.next = prev. Skipping it disconnects the rest of the list before you have walked into it.

17. What are the costs?

Answer

time, space — the entire value of doing it in place.

18. Summarise the pattern in one sentence.

Answer

Carry three pointers and flip one link per step — save the way forward before you overwrite it, and the old head becomes the new tail.


Scorecard

PassDateGot itCloseBlankQuestions missed
1
2
3

↑ Back to LeetCode Patterns — University-Style Teaching Notes