What if you could flip a chain of items instantly without breaking a single link?
Why Reverse a Doubly Linked List in DSA C++?
Imagine you have a paper chain made of links, and you want to flip it so the last link becomes the first. Doing this by hand means unhooking each link and reattaching it in reverse order.
Manually reversing the chain is slow and easy to mess up. You might lose track of links or break the chain. It takes a lot of time and careful handling.
Reversing a doubly linked list swaps the direction of links automatically by changing pointers. This way, the last element becomes the first without breaking the chain or losing any links.
Node* reverseList(Node* head) {
// Manually detach and reattach each node
// Complex and error-prone
}Node* reverseList(Node* head) {
Node* current = head;
Node* temp = nullptr;
while (current != nullptr) {
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}
if (temp != nullptr) head = temp->prev;
return head;
}This lets you quickly flip the order of data in a list, enabling efficient backward and forward traversals.
Think of a music playlist where you want to play songs in reverse order without creating a new list. Reversing the doubly linked list of songs makes this easy.
Manual reversal is slow and risky.
Pointer swapping reverses the list safely and quickly.
Useful for reversing order without extra memory.
Practice
Solution
Step 1: Understand the structure of a doubly linked list
A doubly linked list has nodes with pointers to both previous and next nodes.Step 2: Define reversing operation
Reversing means swapping the direction so the last node becomes the first and vice versa.Final Answer:
To make the last node the first and the first node the last -> Option AQuick Check:
Reverse = swap start and end [OK]
- Confusing reversing with deleting nodes
- Thinking reversing sorts the list
- Assuming it changes list type
next and prev pointers of a node curr in C++?Solution
Step 1: Understand pointer swapping
Swapping two pointers requires a temporary storage or a swap function to avoid overwriting.Step 2: Use std::swap for safe swapping
std::swap exchanges values safely without losing data.Final Answer:
std::swap(curr->next, curr->prev); -> Option BQuick Check:
Use std::swap to exchange pointers [OK]
- Assigning one pointer to another without temp causes data loss
- Swapping pointers manually without temp variable
- Confusing assignment with swapping
struct Node {
int data;
Node* next;
Node* prev;
};
// List: 1 <-> 2 <-> 3
Node* head = new Node{1, nullptr, nullptr};
Node* second = new Node{2, nullptr, head};
head->next = second;
Node* third = new Node{3, nullptr, second};
second->next = third;
// Reverse function
Node* reverse(Node* head) {
Node* curr = head;
Node* temp = nullptr;
while (curr != nullptr) {
temp = curr->prev;
curr->prev = curr->next;
curr->next = temp;
curr = curr->prev;
}
if (temp != nullptr) head = temp->prev;
return head;
}
head = reverse(head);
// Print list
Node* tempPrint = head;
while (tempPrint != nullptr) {
std::cout << tempPrint->data << " ";
tempPrint = tempPrint->next;
}Solution
Step 1: Trace pointer swaps in reverse function
Each node swaps its next and prev pointers, then moves to original next (now prev).Step 2: Update head to new first node
After loop, head is updated to last processed node's prev, which is the original tail.Final Answer:
3 2 1 -> Option CQuick Check:
Reversed list prints from tail to head [OK]
- Not updating head after reversal
- Confusing next and prev pointers
- Printing from old head instead of new
Node* reverse(Node* head) {
Node* curr = head;
Node* temp = nullptr;
while (curr != nullptr) {
temp = curr->prev;
curr->prev = curr->next;
curr->next = temp;
curr = curr->next; // Error here
}
if (temp != nullptr) head = temp->prev;
return head;
}Solution
Step 1: Analyze pointer movement in loop
After swapping next and prev, moving curr to curr->next is wrong because next and prev are swapped.Step 2: Correct pointer update
We must move curr to curr->prev (original next) after swapping.Final Answer:
Change 'curr = curr->next;' to 'curr = curr->prev;' -> Option DQuick Check:
Move curr along swapped pointers [OK]
- Moving curr to next instead of prev after swap
- Not updating head correctly
- Ignoring pointer swap effects
Solution
Step 1: Change function return type
Use std::pair to return both new head and count.Step 2: Count nodes during reversal
Initialize count=0; increment count inside the reversal loop for each node processed.Step 3: Return both values
Return std::make_pair(new_head, count) after reversal completes.Final Answer:
Modify the function to return a pair, increment count inside the loop, and return both. -> Option AQuick Check:
Return both head and count using pair [OK]
- Using global variables instead of return values
- Counting nodes before reversal only
- Trying recursion without changing return type
Ready to prove it?
Pass the test and this pattern moves unknown
