Reverse Linked List

EasyLinked List · In-place Reversal🧩 Linked List TechniquesLeetCode #206 ↗Updated

Problem

Given the head of a singly linked list, flip the direction of every link so the last node becomes the first, and return the new head. An empty list stays empty.
Input[1,2,3,4,5]Output[5,4,3,2,1]
Input[1,2]Output[2,1]
Input[]Output[]

0 ≤ n ≤ 5000  ·  -5000 ≤ Node.val ≤ 5000  ·  Follow-up: solve it both iteratively and recursively.

Try it yourself

Java
class Solution {
    public ListNode reverseList(ListNode head) {
        
    }
}
Your code is saved in this browser.Submit on LeetCode ↗
Key insight
You never need to move nodes — just turn each next arrow around. Before flipping curr.next, save it, or you lose the rest of the list.
PatternLinked List Techniques

Instinct Nodes joined by next pointers and the task is to reverse, splice, merge, reorder or remove in place - grab a dummy head and rewire pointers carefully.

Memory hack Save next, flip, step - never let go of a rope before you've grabbed the next one.

Open the Linked List Techniques guide →

Approaches

Brute ForceStack of Nodes, then Relink

Time O(n)Space O(n)

A stack reverses order for free: push every node, then pop them one by one and chain them in pop order. The last popped node (old head) must get next = null, otherwise the list ends in a cycle.

Java
public ListNode reverseList(ListNode head) {
    if (head == null) return null;
    Deque<ListNode> stack = new ArrayDeque<>();
    for (ListNode c = head; c != null; c = c.next) stack.push(c);
    ListNode newHead = stack.pop(), cur = newHead;
    while (!stack.isEmpty()) {
        cur.next = stack.pop();
        cur = cur.next;
    }
    cur.next = null; // old head is now the tail
    return newHead;
}

Obvious and correct, but stores every node a second time.

At a glance

LevelApproachTimeSpace
Brute ForceStack of Nodes, then RelinkO(n)O(n)
BetterRecursion (Reverse the Rest First)O(n)O(n)
OptimalIterative Three PointersO(n)O(1)
Common pitfalls
  • Overwriting curr.next before saving it — the rest of the list is lost.
  • Returning head or curr instead of prev.
  • Recursive version: forgetting head.next = null creates a 2-node cycle.
Interview tips
  • Draw 3 boxes and arrows; narrate save → flip → advance.
  • Offer both versions (it is the stated follow-up) and compare stack usage.
  • Test aloud on empty, single-node and two-node lists.
Pattern
In-place pointer reversal (prev/curr/next) → reverse a whole list, a sub-range or fixed-size groups.
My notes saved in this browser