The Runtime Theory
easyleetcode#linked-list

Reverse Linked List

Reverse a singly linked list in-place with an iterative three-pointer technique — the canonical linked-list interview problem.

The Runtime Theory Team1 min read
Solve it

solving happens on the judge — come back and mark it done

Sample cases

in[1,2,3,4,5]

out[5,4,3,2,1]

in[1,2]

out[2,1]

in[]

out[]

Given the head of a singly linked list, reverse the list in place and return the new head. The machine must walk the chain once, flipping each node's next pointer to point backward, without allocating a second list.

The key insight: a singly linked list node only knows its successor, so when you redirect a next pointer you lose access to the rest of the list. You must capture the next node before rewriting the pointer — that is what the third pointer is for.

Approach:

  1. Initialize prev = None and curr = head.
  2. For each node: save next_node = curr.next, then set curr.next = prev, then advance prev = curr and curr = next_node.
  3. When curr reaches None, prev is the new head.
python
def reverse_list(head):
    prev = None
    curr = head
    while curr:
        next_node = curr.next
        curr.next = prev
        prev = curr
        curr = next_node
    return prev

Time: O(n), one pass. Space: O(1), no extra structure.

Trickiest edge case: the empty list — the loop never runs and prev stays None, which is correctly the return value. A recursive variant is elegant but costs O(n) stack space; the iterative version is what you want in production.

More practice in this topic

One dispatch a week

The trace behind each problem, the tradeoff that explains it, and one technical dispatch per week — no noise.

One technical dispatch per week. No noise.