DSA AnimatorDSA animations
Reverse Linked List LC #206 Easy Linked List · In-place
Problem

Given the head of a singly linked list, reverse the list, and return the reversed list. Do it in-place with O(1) extra space.

Example 1
Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
Example 2
Input: head = [1,2]
Output: [2,1]
Constraints: 0 ≤ n ≤ 5000  |  −5000 ≤ Node.val ≤ 5000
Try Examples
Custom:
Linked List
List reversed — new head =
Pointer State
prev
null
curr
—
nxt
—
Step Logic
Press ▶ Play or Next Step to begin the animation.
🎉
Ready
0 / 0
Select an example above and press Play.
Algorithm
1
Init prev = null, curr = head
2
Loop while curr != null
3
Save nxt = curr.next (don't lose the rest of the list)
4
Flip pointer: curr.next = prev
5
Advance: prev = curr, curr = nxt
6
Return prev — the new head of the reversed list
Time
O(n)
Space
O(1)
Why It Works

At every step, everything behind curr is already a valid reversed sub-list whose new head is prev. By saving nxt before clobbering curr.next, we never lose the forward chain. When curr reaches null, the entire list has been reversed and prev points to the new head.

How to solve Reverse Linked List

LeetCode 206 · Easy · Linked List · Pattern: Pointer Reversal

Why the obvious approach is slow

Pushing every value onto a stack and rebuilding uses O(n) extra space. We can just flip the arrows in place.

The key idea

Keep three pointers: previous, current and next. Save next, then point current back at previous. Move previous and current one step forward, and repeat until current is null.

Remember

Save next, flip the arrow, then step forward. Previous ends up as the new head.

Run the animation above on your own input, or watch the 1:05 video explanation of Reverse Linked List with a dry run and Java code.

More Linked List problems