DSA Crash Course
17 FAANG Patterns

Pattern 6: In-place Linked List Reversal

Mark when done:

Deep Dive: The Three-Pointer Dance

Reverse Sublist Between Positions m=2 and n=4

Original: 1 -> 2 -> 3 -> 4 -> 5

Step 1: Walk to node before m: conn = node 1
Step 2: Reverse nodes 2->3->4 using prev/curr/next:
        1 -> 2 <- 3 <- 4    5
             |              ^
             tail          prev=4, curr=5

Step 3: Connect: conn.next = prev (4), tail.next = curr (5)
Result: 1 -> 4 -> 3 -> 2 -> 5

Pattern: Find the "connection point" before the reversal, remember the "tail" (first reversed node becomes last), reverse the segment, then reconnect.


Signal: Reverse all or part of a linked list Template: reverse_linked_list() in templates.py

When to Use

  • "Reverse a linked list"
  • "Reverse nodes k at a time"
  • "Reverse between position m and n"

The Template (Memorize This)

prev, curr = None, head
while curr:
    nxt = curr.next
    curr.next = prev
    prev = curr
    curr = nxt
return prev  # new head

Re-solve from Phase 1-2 (TIMED)

ProblemSourceTarget TimeDone?
Reverse Linked ListPhase 1.58 min[ ]
Reorder ListPhase 1.520 min[ ]

New Practice Problems

Add problems to solutions/ and enrich as needed.


First-time Recognition Signals

When you read a brand-new problem statement, this pattern is the right tool if you see:

  • "Reverse a linked list (whole, or between positions m and n)" — the textbook signal.
  • "Reverse every K nodes / swap nodes in pairs" — repeated reversal of fixed-size segments, stitching the segments back together.
  • "Reorder list, palindrome check, or rotate by K" on a linked list — the natural sub-step is reversing the second half in place.
  • An explicit O(1) extra space constraint on a linked-list reordering problem — that constraint forces pointer rewiring rather than copying values into an array.

Anti-signals (looks like this pattern, isn't)

  • "Reverse a string or array" — use two-pointer swap; pointer rewiring is unnecessary when you have random access.
  • "Reverse the level order of a binary tree" — that is BFS with a deque, not linked-list reversal.
  • "Print a linked list in reverse" with no requirement to mutate it — a stack or recursion is simpler and safer than rewiring .next pointers.