AlgoMaster Logo

Reverse Linked List

easyFrequency6 min readUpdated June 23, 2026

Understanding the Problem

Reversing a linked list means flipping the direction of every pointer. In the original list, each node points to the next one: 1->2->3->4->5->null. After reversal, each node should point to the previous one: 5->4->3->2->1->null. The old tail becomes the new head.

The challenge is that singly linked lists only have forward pointers. You can't look backward. So to reverse a node's pointer, you need to have already saved a reference to the previous node before you move forward. If you don't, you lose track of where to point.

Reversal is a building block in many other linked list problems, including reverse nodes in k-group, palindrome linked list, and reorder list.

Key Constraints:

  • 0 <= n <= 5000 → The list can be empty, so the algorithm must return null when head is null.
  • Singly linked list → Nodes have only forward pointers. This is the core constraint that makes reversal non-trivial, since reversing a node's pointer requires a reference to the node that should precede it.

Approach 1: Iterative (Three Pointers)

Intuition

Walk through the list one node at a time, and at each node, flip its next pointer to point backward instead of forward. This needs three pointers:

  • prev -- the node the current node should point to (starts as null, since the original head becomes the new tail and must point to null)
  • curr -- the node being processed
  • next -- a saved copy of curr.next, captured before the pointer is overwritten

The moment we set curr.next = prev, the original curr.next is gone. Saving it first in next preserves the only reference to the rest of the list.

Algorithm

  1. Initialize prev = null and curr = head.
  2. While curr is not null:
    • Save next = curr.next (so we don't lose the rest of the list).
    • Reverse the pointer: curr.next = prev.
    • Move prev forward: prev = curr.
    • Move curr forward: curr = next.
  3. When curr is null, prev points to the last node we processed, which is the new head. Return prev.

Example Walkthrough

1Initialize: prev=null, curr=1
1
curr
2
3
4
5
null
1/7

Code

This approach is optimal in both time and space. O(n) time is required since every node must be visited, and O(1) extra space cannot be improved.

The follow-up asks for a recursive solution. The next approach expresses reversal as a recursive subproblem: reverse the rest of the list, then fix the current node's pointer.

Approach 2: Recursive

Intuition

The reversal splits into a subproblem and one fix-up step: reverse everything after the current node, then make the next node point back to the current node and set the current node's next to null.

Take the list 1->2->3->4->5. Once everything after node 1 is reversed, the list is 1->2<-3<-4<-5. Node 1 still points forward to node 2, and node 2 is now the tail of the reversed sublist (its next is null). Setting 2.next = 1 and 1.next = null reverses the final pointer and completes the list.

The base case is a list of 0 or 1 nodes, which is already reversed and returned unchanged. From there, the recursion unwinds from the tail back to the head, fixing one pointer per level. The new head, found at the deepest call, is passed back up unchanged at every level.

Algorithm

  1. Base case: if head is null or head.next is null, return head (a list of 0 or 1 nodes is already reversed).
  2. Recursively reverse the sublist starting from head.next. This returns the new head of the reversed sublist.
  3. Fix the pointers: head.next.next = head (make the next node point back to current).
  4. Set head.next = null (current node becomes the new tail of its portion).
  5. Return the new head (which was returned by the recursive call).

Example Walkthrough:

1Start: reverseList(1->2->3->4->5)
1
head
2
3
4
5
null
1/7

Code

Approach 3: Stack-Based

Intuition

A stack reverses order by construction: the last item pushed is the first popped. Pushing every node, then popping them, yields the nodes in reverse order. Linking each popped node to the next popped node rebuilds the list backward.

This approach makes the reversal explicit rather than rewiring pointers in place. It uses O(n) extra space for the stack, so it is not an improvement over the iterative method, but it shows how an explicit data structure can stand in for the recursion stack, and it is a useful pattern when the same nodes must be processed in reverse later in a larger problem.

Algorithm

  1. If head is null, return null.
  2. Push every node onto a stack while walking the list forward.
  3. Pop the top node; this is the new head.
  4. Pop nodes one by one. For each popped node, set the previous popped node's next to it.
  5. After the loop, set the last popped node's next to null to terminate the list.
  6. Return the new head.

Example Walkthrough

1Push all nodes onto the stack: bottom [1, 2, 3, 4, 5] top
1
2
3
4
5
null
1/6

Code