← DSA Atlas
Dedicated problem page · #206

Reverse Linked List

EasyLinked Lists and Pointer ManipulationIterative pointer reversalLinked list three-pointer swap
Solve on LeetCode ↗
206
EasyLinked Lists and Pointer ManipulationLinked list three-pointer swapIterative pointer reversal

Reverse Linked List

Given the head of a singly linked list, reverse the list so that the last node becomes the new head and every next pointer points to the node that used to precede it. Return the new head.

Open official problem prompt ↗
In plain English

Produce the same nodes linked in the opposite order, returning the former tail as the new head, without allocating a second list.

Picture it like this

Like reversing a chain of train cars by re-coupling each car to the one behind it — you must hold the next car before you unhook it, or it rolls away.

Example
Input
head = [1, 2, 3, 4, 5]
Output
[5, 4, 3, 2, 1]
Why
Each node's next pointer is flipped, so traversal now starts at 5 and ends at 1.
Constraints
The number of nodes is in the range [0, 5000]-5000 <= Node.val <= 5000
Pattern lesson

See the pattern, then code

Iterative pointer reversal
Recognition clue

The prompt asks to invert the direction of a singly linked list in place with no value copying — the classic signal for the prev/curr/next reversal walk.

Linked Lists and Pointer Manipulation

Reversal, cycle detection, merging, reordering, or O(1)-space sequence edits.. You only ever need three references: the node before you (prev), the node you are rewiring (curr), and a saved handle to the rest of the list (next) so you don't lose it when you flip curr.next.

New words, made simpleKnow these before the algorithm
Node
A record holding a value and a next pointer to the following node.
Head
Reference to the first node; None when the list is empty.
In-place
Rearranging existing nodes using O(1) extra memory rather than building a new list.
Approaches from first idea to best ideaCompare the trade-offs
ApproachHow it thinksCost
Copy values into an array and rebuild

Wastes memory and allocations for something the pointers can do directly.

Walk the list into a list, reverse it, then build new nodes.

Time O(n)Space O(n)
The rule we keep true

Invariant

After processing k nodes, prev heads a correctly reversed sublist of those k nodes and curr points at the first not-yet-reversed node.

Why this is correct

Reasoning

Every original edge a->b is rewritten exactly once as b->a, and saving nxt before overwriting curr.next guarantees the remaining list is never orphaned, so all n nodes are relinked in reverse.

The algorithm in three movesSay these aloud before coding
1Initialize prev to None and curr to head

prev=None, curr=1

2Save curr.next before overwriting it

flip 1->None, prev=1, curr=2

3Point curr.next back to prev

flip 2->1, prev=2, curr=3

4Advance prev and curr one step

... return 5

5When curr is None, prev is the new head

Visual trace

Follow the data, one decision at a time

Input snapshotHighlighted cells are involved in this example
10
21
32
43
54
1 · Readcurr=1
2 · AskSave the rest before flipping?
3 · Update stateprev=None
4 · Resultnxt=2, set 1.next=None, prev=1, curr=2
Key takeaway

The boundary between reversed (left of curr) and untouched (curr onward) nodes moves rightward each step.

Code walkthrough

Read the solution in small chunks

Python 3

Do not memorize the whole program. Connect each group of lines to one job in the algorithm.

  1. 1
    Lines 3-4Seed the pointers

    prev starts empty (the reversed list is empty) and curr starts at the head.

  2. 2
    Lines 5-9Flip one edge per iteration

    Cache next, redirect curr.next to prev, then slide both pointers forward.

  3. 3
    Lines 10Return the new head

    When curr falls off the end, prev is the old tail — now the head.

Make it stick

Edge cases, mistakes, and one self-check

E

Edge cases to test

  • Empty list (head is None) returns None
  • Single node returns itself unchanged
  • Two-node list simply swaps the pair
!

Common beginner mistakes

  • Overwriting curr.next before saving nxt, which loses the rest of the list
  • Returning head instead of prev (head is now the tail)
  • Forgetting to advance curr, causing an infinite loop
Check your understanding

Why must nxt be captured before the line curr.next = prev?