Skip to content
16-Week DSA Roadmap
LeetCode #206EasyProblem 4 of 8

Reverse Linked List

Turn 1 → 2 → 3 into 3 → 2 → 1 by flipping every arrow, using three pointers (or recursion).

  • Reversing a linked list
  • Pointer rewiring
  • Recursion

Before this, know: What a linked list is (see the week 4 overview), Following node.next in a loop

The problem in plain English

You get head, the first node of a singly linked list: a chain of nodes where each node holds a value (val) and an arrow to the next node (next). The last node’s next is null (None in Python), meaning “nothing after me”. Reverse the list and return the new head.

before
1234
after
4321
Before: 1 → 2 → 3 → 4 → null. After: 4 → 3 → 2 → 1 → null. The nodes are the same; only the arrows change direction.

The rules that matter:

  • The list can be empty (head is null) or have one node. Both are their own reverse.
  • Up to 5,000 nodes. We should change the existing nodes’ arrows, not build a new list.

Before you code: think it through

Reversing means: every arrow A → B becomes B → A. Walk the list once, and at each node point its arrow backwards, at the node we just came from. That needs three pointers (a pointer here is just a variable that holds a node):

  • prev: the node behind us (the start of the part already reversed),
  • curr: the node we’re flipping now,
  • next: the node after curr, saved before we flip, because flipping curr.next destroys our only link to the rest of the list.

Pause and think

We're at curr = 2 in 1 → 2 → 3, and we write curr.next = prev (pointing 2 back at 1) WITHOUT saving next first. What goes wrong?

Approach 1: Copy the values out

The simplest idea doesn’t touch the arrows at all: walk the list and copy every value into an array, then walk it again and write the values back, last one first. pop() removes and returns the last item of an array, which hands the values back in reverse.

Copy the values out, write them back reversed

head = [1, 2, 3, 4]

1234
variables
node
1node
values (extra array)
  1. 10
  • Copying this node
  • Value rewritten
  • Done

Pass 1: copy node 1's value into the array. The array now holds [1].

Step 1 / 9

Which line is running?

function reverseList(head) {  const values = [];  let node = head;  while (node !== null) { // pass 1: copy every value outRunning now:     values.push(node.val);    node = node.next;  }  node = head;  while (node !== null) { // pass 2: write them back, last value first    node.val = values.pop();    node = node.next;  }  return head;}

Copy values, write them back reversed

function reverseList(head) {  const values = [];  let node = head;  while (node !== null) { // pass 1: copy every value out    values.push(node.val);    node = node.next;  }  node = head;  while (node !== null) { // pass 2: write them back, last value first    node.val = values.pop();    node = node.next;  }  return head;}
def reverse_list(head):    values = []    node = head    while node is not None:  # pass 1: copy every value out        values.append(node.val)        node = node.next    node = head    while node is not None:  # pass 2: write them back, last value first        node.val = values.pop()        node = node.next    return head

Why isn’t it good enough?

It’s O(n) time, which is fine, but it needs an extra array as long as the list: 5,000 nodes, 5,000 extra slots. It also changes the values, not the list. In real code nodes often carry more than a number (or other code holds references to them), and an interviewer will ask you to reverse the links. We can do that with three variables and no array.

The pattern: reversing a linked list

Pattern

Reversing a linked list

Template

Use it when: you need a list (or part of one) backwards, in place, without extra memory.

Signal words

  • “reverse the list”
  • “reverse between”
  • “palindrome linked list”
  • “reorder”
  • “in place”

Approach 2: Iterative, with prev / curr / next

  1. prev = null (the reversed part is empty) and curr = head.
  2. While curr isn’t null:
  3. Save next = curr.next.
  4. Flip: curr.next = prev.
  5. Step prev = curr, then curr = next.
  6. When the loop ends, return prev: it’s the old tail, now the head.

Watch the arrows: each flipped arrow curves back underneath the row. Grey nodes are the part already reversed (prev’s list). The amber node is the saved next, our lifeline to the rest of the list.

Iterative: prev, curr, next

head = [1, 2, 3, 4]

1234
variables
prev
null
curr
1node
  • curr (being flipped)
  • next (saved)
  • Already reversed (prev's list)
  • Arrow just flipped
  • Reversed list

We will flip every arrow so it points backwards. prev holds the part we have already reversed. At the start that part is empty, so prev = null.

Step 1 / 19

Which line is running?

function reverseList(head) {Running now:   let prev = null; // the reversed part (empty at first)  let curr = head; // the node we are flipping now  while (curr !== null) {    const next = curr.next; // 1. save the rest of the list    curr.next = prev; // 2. flip this arrow backwards    prev = curr; // 3. step prev forward    curr = next; // 4. step curr forward  }  return prev; // the old tail is the new head}

Approach 3: Recursion

Recursion means a function calling itself on a smaller version of the same problem. Here: “to reverse the list starting at head, first reverse everything after head, then hang head on the end.”

The trick is to trust that reverseList(head.next) works (like trusting a helper), then fix up just one arrow. When the recursive call returns, head.next still points at its old neighbour, which is now the tail of the reversed part. So head.next.next = head hangs head after it, and head.next = null makes head the new end.

Every call that hasn’t finished waits on the call stack (a pile of paused function calls; the newest is on top). Watch it grow down the list, then shrink as each call fixes its arrow.

Recursive, with the call stack

head = [1, 2, 3]

call stack
  1. reverseList(1)head = 1
123
variables
head
1node
newHead
null
  • This call's head
  • head.next
  • Arrow just changed
  • newHead

Call reverseList(1). First question: is the list empty or a single node?

Step 1 / 15

Which line is running?

function reverseList(head) {Running now:   if (head === null || head.next === null) {    return head; // 0 or 1 node: already reversed  }  const newHead = reverseList(head.next); // reverse everything after head  head.next.next = head; // the node after head points back at head  head.next = null; // head is now the tail of the reversed part  return newHead;}

Recursive

function reverseList(head) {  if (head === null || head.next === null) {    return head; // 0 or 1 node: already reversed  }  const newHead = reverseList(head.next); // reverse everything after head  head.next.next = head; // the node after head points back at head  head.next = null; // head is now the tail of the reversed part  return newHead;}
def reverse_list(head):    if head is None or head.next is None:        return head  # 0 or 1 node: already reversed    new_head = reverse_list(head.next)  # reverse everything after head    head.next.next = head  # the node after head points back at head    head.next = None  # head is now the tail of the reversed part    return new_head

The code, line by line

Iterative (the one to use)

function reverseList(head) {  let prev = null; // the reversed part (empty at first)  let curr = head; // the node we are flipping now  while (curr !== null) {    const next = curr.next; // 1. save the rest of the list    curr.next = prev; // 2. flip this arrow backwards    prev = curr; // 3. step prev forward    curr = next; // 4. step curr forward  }  return prev; // the old tail is the new head}
def reverse_list(head):    prev = None  # the reversed part (empty at first)    curr = head  # the node we are flipping now    while curr is not None:        nxt = curr.next  # 1. save the rest of the list        curr.next = prev  # 2. flip this arrow backwards        prev = curr  # 3. step prev forward        curr = nxt  # 4. step curr forward    return prev  # the old tail is the new head

The line numbers match in both languages. On LeetCode this sits inside var reverseList = function(head) {...} (JavaScript) or class Solution with def reverseList(self, head) (Python), and LeetCode defines ListNode for you.

  • Line 2: prev starts as null. The first node will become the last node, and the last node must point to null, so null is exactly what it should point at after its flip.
  • Line 3: curr starts at the head.
  • Line 4: keep going while there’s a node to flip. An empty list skips the loop entirely.
  • Line 5: save the rest of the list. Python names it nxt because next is a built-in Python function and shadowing it is bad style.
  • Line 6: the actual reversal: this node now points backwards.
  • Lines 7–8: move both pointers one node forward. Order matters: prev must take curr’s value before curr moves on.
  • Line 10 (JS) / 9 (Py): curr is null, so prev is the last node we flipped, the new head. Returning head here is a classic bug: the old head is now the tail.

For the recursive version: line 2 is the base case (empty or one node is already reversed), JS 5 / Py 4 reverses the rest, JS 6–7 / Py 5–6 fix the two arrows, and the same newHead is passed all the way back up.

Dry run

head = [1, 2, 3, 4], one row per trip around the loop (iterative):

currnext (saved)flipprev afterreversed partrest
121 → null112 → 3 → 4
232 → 122 → 13 → 4
343 → 233 → 2 → 14
4null4 → 344 → 3 → 2 → 1(empty)
null——4return prev = 4—
head = [1, 2, 3, 4]

At every row the list is split into two healthy lists: the reversed part (starting at prev) and the rest (starting at next). Nothing is ever lost.

Complexity

Copy values out and back

Time

O(n)

Two passes over the n nodes.

Space

O(n)

An extra array holding every value.

Iterative prev / curr / next

Time

O(n)

One pass; each node is flipped once.

Space

O(1)

Three pointer variables, whatever the list's length.

Recursive

Time

O(n)

One call per node.

Space

O(n)

The call stack holds one paused call per node: 5,000 nodes, 5,000 frames.

Common mistakes and edge cases

What to say out loud:

  1. “I’ll reverse the links in place with three pointers: prev, curr and next.”
  2. “Save next first, flip curr.next to prev, then advance both.”
  3. “O(n) time, O(1) space. The recursive version is O(n) space because of the call stack.”
  4. Draw three boxes and walk one iteration before writing code.

Likely follow-ups:

  • “Reverse only positions left to right.” (Reverse Linked List II, LeetCode 92) Walk to the node before left, reverse the next right − left + 1 nodes the same way, then reconnect both ends.
  • “Can you do it recursively?” Approach 3 above.

Related problems:

  • Reorder List: reversing the second half is one of its three phases.
  • Palindrome Linked List (LeetCode 234): find the middle, reverse the second half, compare.
  • The full template and more is in the week 4 overview.