How to Reverse a Linked List in C++: Simple Guide and Example
To reverse a linked list in C++, iterate through the list while changing each node's
next pointer to point to the previous node. Use three pointers: prev, current, and next to track nodes during reversal.Syntax
The basic syntax to reverse a singly linked list involves a loop that updates pointers to reverse the direction of the list.
- prev: Tracks the previous node (starts as
nullptr). - current: Tracks the current node being processed.
- next: Temporarily stores the next node to move forward safely.
cpp
Node* reverseList(Node* head) {
Node* prev = nullptr;
Node* current = head;
Node* next = nullptr;
while (current != nullptr) {
next = current->next; // Save next node
current->next = prev; // Reverse pointer
prev = current; // Move prev forward
current = next; // Move current forward
}
return prev; // New head of reversed list
}Example
This example creates a simple linked list, reverses it using the reverseList function, and prints the list before and after reversal.
cpp
#include <iostream>
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
Node* reverseList(Node* head) {
Node* prev = nullptr;
Node* current = head;
Node* next = nullptr;
while (current != nullptr) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
void printList(Node* head) {
Node* temp = head;
while (temp != nullptr) {
std::cout << temp->data << " ";
temp = temp->next;
}
std::cout << std::endl;
}
int main() {
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
std::cout << "Original list: ";
printList(head);
head = reverseList(head);
std::cout << "Reversed list: ";
printList(head);
return 0;
}Output
Original list: 1 2 3 4
Reversed list: 4 3 2 1
Common Pitfalls
Common mistakes when reversing a linked list include:
- Not updating the
nextpointer before changing it, which causes loss of the rest of the list. - Forgetting to return the new head (
prev) after reversal. - Not handling empty lists (
head == nullptr) properly.
Always save the next node before changing pointers to avoid breaking the list.
cpp
/* Wrong way: Losing the rest of the list */ Node* reverseListWrong(Node* head) { Node* prev = nullptr; Node* current = head; while (current != nullptr) { current->next = prev; // Changed pointer before saving next prev = current; current = current->next; // current->next is now prev, leads to loop or nullptr } return prev; } /* Right way: Save next before changing pointer */ Node* reverseListRight(Node* head) { Node* prev = nullptr; Node* current = head; Node* next = nullptr; while (current != nullptr) { next = current->next; // Save next current->next = prev; // Reverse pointer prev = current; current = next; // Move forward } return prev; }
Quick Reference
- Use three pointers:
prev,current, andnext. - Always save
nextbefore changingcurrent->next. - Return
prevas the new head after reversal. - Handle empty lists by checking if
headisnullptr.
Key Takeaways
Use three pointers to reverse the linked list safely without losing nodes.
Always save the next node before changing the current node's next pointer.
Return the previous pointer as the new head after reversal.
Check for empty lists to avoid null pointer errors.
Test your reversal function with simple lists to ensure correctness.