Reverse Linked List

Easy
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Reverse Linked List

Approach

Walk the list with three pointers: prev (initially null), curr (initially head), and next (saved before rewiring). At each step, point curr.next back to prev, then advance prev and curr forward. When curr is null, prev is the new head.

Complexity Analysis

Time
O(n)
Space
O(1)

Pattern

Linked List (Reversal)

Why It Works

Each node is visited exactly once and its next pointer is redirected to the previous node. By the end of the traversal prev points to the last node visited, which is the new head of the reversed list.

Updated Feb 2026