What does it solve?
Given the head of a singly linked list, reverse all the next-pointers so that the list runs backwards — the old tail becomes the new head — entirely in-place with no extra memory.
At each node, we redirect its next pointer to the previous node. Three pointers — prev, curr, and next — keep track of the surrounding context so nothing is lost.
The core idea
// three pointers, one pass:
prev = NULL
curr = head
while curr != NULL:
next = curr→next // save
curr→next = prev // reverse
prev = curr // advance
curr = next // advance
return prev // new head
prev = NULL
curr = head
while curr != NULL:
next = curr→next // save
curr→next = prev // reverse
prev = curr // advance
curr = next // advance
return prev // new head
Complexity
Time complexity
O(n)
One pass — every node is visited exactly once and its pointer reversed in constant time.
Space complexity
O(1)
Only three pointer variables — prev, curr, next. No new nodes or arrays created.
Examples & edge cases
Live animation
prev
curr
next
reversed pointer
fully reversed node
Step — of —
prev
—
—
curr
—
—
next
—
—
Step
—
—
Select an example and press Play to start.
✓ Complete
Speed
Medium
Step-by-step table
| Step | Sub-step | prev | curr | next | Action |
|---|
C++ implementation
reverse_linked_list.cpp
#include <bits/stdc++.h> using namespace std; struct ListNode { int val; ListNode* next; ListNode(int v) : val(v), next(nullptr) {} }; ListNode* reverseList(ListNode* head) { ListNode* prev = nullptr; ListNode* curr = head; while (curr != nullptr) { ListNode* next = curr->next; // 1. save next curr->next = prev; // 2. reverse pointer prev = curr; // 3. advance prev curr = next; // 4. advance curr } return prev; // new head }
My Notes