Reversing a Linked List
Reversing a linked list means changing the direction of its links so that the last node becomes the first node and the first node becomes the last node. It is an important linked list operation and a common coding interview question. We can reverse a linked list using iteration or recursion.
1. What Is Reversing a Linked List?
A singly linked list consists of nodes where each node stores data and a pointer to the next node. Normally, every node points towards the next node in the sequence.
Reversing changes these next pointers so that each node points to its previous node instead.
Example
Original linked list:
10 -> 20 -> 30 -> 40 -> nullptr
After reversing:
40 -> 30 -> 20 -> 10 -> nullptr
The node containing 40 becomes the new head, and the original head becomes the last node.
2. Why Do We Reverse a Linked List?
Reversing a linked list is useful in many situations:
- Changing the order of elements without creating another list.
- Solving palindrome linked list problems.
- Processing data in reverse order.
- Solving linked list interview questions.
- Understanding pointer manipulation.
- Building more advanced linked list algorithms.
3. Iterative Approach Using Three Pointers
The most common method for reversing a singly linked list is the iterative approach. It uses three pointers:
- prev: Points to the previous node.
- curr: Points to the current node.
- nextNode: Temporarily stores the next node.
Initial Structure
prev = nullptr
curr
|
v
10 -> 20 -> 30 -> nullptr
Algorithm
- Initialize prev to nullptr.
- Set curr to head.
- Store curr->next in nextNode.
- Change curr->next to prev.
- Move prev to curr.
- Move curr to nextNode.
- Repeat until curr becomes nullptr.
- Finally, update head to prev.
C++ Code
Node* reverseList(Node* head) {
Node* prev = nullptr;
Node* curr = head;
while (curr != nullptr) {
Node* nextNode = curr->next;
curr->next = prev;
prev = curr;
curr = nextNode;
}
return prev;
}
Explanation
The algorithm processes one node at a time. Before changing the current node's next pointer, it saves the original next node. This prevents losing access to the remaining list.
curr->next before reversing the pointer.
Otherwise, the remaining nodes may become inaccessible.
4. Dry Run of the Iterative Approach
Consider the following linked list:
10 -> 20 -> 30 -> nullptr
Initially:
- prev = nullptr
- curr = 10
Iteration 1
- Store nextNode = 20.
- Change 10->next to nullptr.
- Move prev to 10.
- Move curr to 20.
10 -> nullptr
20 -> 30 -> nullptr
Iteration 2
- Store nextNode = 30.
- Change 20->next to 10.
- Move prev to 20.
- Move curr to 30.
20 -> 10 -> nullptr
30 -> nullptr
Iteration 3
- Store nextNode = nullptr.
- Change 30->next to 20.
- Move prev to 30.
- Move curr to nullptr.
30 -> 20 -> 10 -> nullptr
Final Step
The loop ends because curr is nullptr. The prev pointer now points to the new first node.
Reversed List: 30 -> 20 -> 10 -> nullptr
5. Iterative Approach Using a Complete C++ Program
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* reverseList(Node* head) {
Node* prev = nullptr;
Node* curr = head;
while (curr != nullptr) {
Node* nextNode = curr->next;
curr->next = prev;
prev = curr;
curr = nextNode;
}
return prev;
}
void display(Node* head) {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
int main() {
Node* head = new Node{10, nullptr};
head->next = new Node{20, nullptr};
head->next->next = new Node{30, nullptr};
head->next->next->next = new Node{40, nullptr};
cout << "Original List: ";
display(head);
head = reverseList(head);
cout << "Reversed List: ";
display(head);
// Free allocated memory
while (head != nullptr) {
Node* temp = head;
head = head->next;
delete temp;
}
return 0;
}
Output
Original List: 10 20 30 40
Reversed List: 40 30 20 10
6. Recursive Approach
We can also reverse a linked list using recursion. In this approach, we recursively reach the last node and then reverse the links while returning from recursive calls.
Basic Idea
- If the list is empty or contains only one node, return head.
- Recursively reverse the list starting from head->next.
- Make the next node point back to the current node.
- Set the current node's next pointer to nullptr.
- Return the new head.
C++ Code
Node* reverseRecursive(Node* head) {
// Base case
if (head == nullptr || head->next == nullptr) {
return head;
}
// Reverse the remaining list
Node* newHead = reverseRecursive(head->next);
// Reverse the current connection
head->next->next = head;
head->next = nullptr;
return newHead;
}
How Recursion Works
Consider this list:
10 -> 20 -> 30 -> nullptr
The recursive calls move towards the last node:
reverseRecursive(10)
|
v
reverseRecursive(20)
|
v
reverseRecursive(30)
|
v
Base case reached
When recursion returns, the links are reversed:
30 -> 20 -> 10 -> nullptr
7. Complete C++ Program Using Recursion
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* reverseRecursive(Node* head) {
if (head == nullptr || head->next == nullptr) {
return head;
}
Node* newHead = reverseRecursive(head->next);
head->next->next = head;
head->next = nullptr;
return newHead;
}
void display(Node* head) {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
int main() {
Node* head = new Node{10, nullptr};
head->next = new Node{20, nullptr};
head->next->next = new Node{30, nullptr};
head->next->next->next = new Node{40, nullptr};
cout << "Original List: ";
display(head);
head = reverseRecursive(head);
cout << "Reversed List: ";
display(head);
// Free allocated memory
while (head != nullptr) {
Node* temp = head;
head = head->next;
delete temp;
}
return 0;
}
Output
Original List: 10 20 30 40
Reversed List: 40 30 20 10
8. Iterative vs Recursive Approach
| Feature | Iterative | Recursive |
|---|---|---|
| Technique | Uses a loop | Uses recursive function calls |
| Time Complexity | O(n) | O(n) |
| Auxiliary Space | O(1) | O(n) call stack |
| Pointer Management | Uses three pointers | Uses recursive calls and pointer updates |
| Stack Usage | No recursion stack | Uses recursion stack |
| Practical Use | Generally preferred for large lists | Useful for understanding recursion |
9. Time and Space Complexity
Iterative Approach
- Time Complexity: O(n), because every node is visited once.
- Auxiliary Space Complexity: O(1), because only a constant number of pointers are used.
Recursive Approach
- Time Complexity: O(n), because every node is processed once.
- Auxiliary Space Complexity: O(n), because recursive calls use the call stack.
10. Important Edge Cases
A correct reversal algorithm must handle the following cases:
Case 1: Empty Linked List
nullptr
The result remains nullptr.
Case 2: Single Node
10 -> nullptr
The list remains unchanged.
Case 3: Two Nodes
Before: 10 -> 20 -> nullptr
After: 20 -> 10 -> nullptr
Case 4: Multiple Nodes
Before:
10 -> 20 -> 30 -> 40 -> nullptr
After:
40 -> 30 -> 20 -> 10 -> nullptr
11. Common Mistakes
1. Not Saving the Next Node
If we change curr->next before saving its original value, we may lose access to the remaining nodes.
2. Forgetting to Move the Pointers
After reversing a connection, both prev and curr must be updated correctly. Otherwise, the loop may not progress.
3. Returning the Wrong Pointer
After iterative reversal, prev points to the new head. Returning the original head is incorrect.
4. Forgetting the Base Case in Recursion
The recursive method must stop when the list is empty or when only one node remains.
5. Forgetting to Set the Last Link to nullptr
In the recursive approach, set head->next to nullptr while unwinding the recursion to avoid creating an incorrect connection or cycle.
12. Applications of Linked List Reversal
- Reversing the order of stored elements.
- Checking whether a linked list is a palindrome.
- Reversing a portion of a linked list.
- Reversing nodes in groups of a fixed size.
- Solving problems involving forward and backward traversal.
- Practising pointer manipulation and recursion.
13. Interview Questions
Q1. What does reversing a linked list mean?
It means changing the direction of the next pointers so that the last node becomes the first node.
Q2. Which pointers are used in iterative reversal?
The three main pointers are prev, curr, and nextNode.
Q3. What is the time complexity of reversing a linked list?
O(n), because every node must be visited once.
Q4. What is the auxiliary space complexity of iterative reversal?
O(1), because it uses only a fixed number of pointers.
Q5. What is the auxiliary space complexity of recursive reversal?
O(n), because the recursive calls use the call stack.
Q6. Why do we use a nextNode pointer?
It saves the original next node before changing the current node's next pointer.
Q7. What does prev point to after the iterative loop?
It points to the new head of the reversed linked list.
Q8. Can we reverse a linked list without creating new nodes?
Yes. We can reverse the existing links by changing the next pointers of the original nodes.
Q9. Which approach is generally more space efficient?
The iterative approach, because it requires O(1) auxiliary space.
Q10. What happens when an empty linked list is reversed?
The result is still nullptr.
14. Practice Problems
- Reverse a singly linked list using an iterative approach.
- Reverse a singly linked list using recursion.
- Reverse a linked list containing only two nodes.
- Reverse a linked list containing duplicate values.
- Reverse a linked list between two given positions.
- Reverse nodes in groups of size k.
- Find whether a linked list is a palindrome using reversal.
- Reverse a doubly linked list.
- Reverse a linked list without changing its node values.
- Compare iterative and recursive linked list reversal.
15. Summary
- Reversing changes the direction of linked list pointers.
- The iterative approach uses prev, curr, and nextNode.
- The iterative approach takes O(n) time and O(1) auxiliary space.
- The recursive approach takes O(n) time and O(n) auxiliary space.
- The new head is the original last node.
- Always save the next node before changing a pointer.
- Handle empty and single-node lists correctly.