Challenge - 5 Problems
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of reversing a doubly linked list
What is the printed state of the doubly linked list after reversing it using the given code?
DSA C++
struct Node {
int data;
Node* prev;
Node* next;
};
Node* reverse(Node* head) {
Node* temp = nullptr;
Node* current = head;
while (current != nullptr) {
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}
if (temp != nullptr) {
head = temp->prev;
}
return head;
}
// Initial list: 1 <-> 2 <-> 3 <-> 4 <-> nullptr
// After reverse, print from head to end: data -> ... -> nullptrAttempts:
2 left
💡 Hint
Think about how the pointers are swapped and how the head is updated after reversal.
✗ Incorrect
The code swaps the prev and next pointers for each node, effectively reversing the list. The new head is the last node processed, which is node with data 4. So the list prints as 4 -> 3 -> 2 -> 1 -> nullptr.
🧠 Conceptual
intermediateUnderstanding pointer swaps in reversing doubly linked list
In the reversal process of a doubly linked list, which pointers are swapped for each node?
Attempts:
2 left
💡 Hint
Think about how the direction of the list is reversed.
✗ Incorrect
Reversing a doubly linked list requires swapping both prev and next pointers for each node to reverse the direction of links.
🔧 Debug
advancedIdentify the error in this reverse function
What error will occur when running this reverse function on a doubly linked list?
DSA C++
Node* reverse(Node* head) {
Node* temp = nullptr;
Node* current = head;
while (current != nullptr) {
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->next; // Note this line
}
if (temp != nullptr) {
head = temp->prev;
}
return head;
}Attempts:
2 left
💡 Hint
Check how the current pointer moves inside the loop.
✗ Incorrect
The line 'current = current->next;' is incorrect because after swapping prev and next, current->next points to the old prev (nullptr for head), causing the loop to exit after one iteration without processing the rest of the list. Thus, it returns the original head without reversing.
❓ Predict Output
advancedResulting list after partial reversal
Given a doubly linked list 1 <-> 2 <-> 3 <-> 4 <-> 5, what is the printed list after reversing only the first three nodes using the code below?
DSA C++
Node* partialReverse(Node* head, int k) { Node* current = head; Node* temp = nullptr; int count = 0; while (current != nullptr && count < k) { temp = current->prev; current->prev = current->next; current->next = temp; current = current->prev; count++; } if (temp != nullptr) { head = temp->prev; } return head; } // Print list from returned head to end
Attempts:
2 left
💡 Hint
Only the first k nodes are reversed, the rest remain in original order.
✗ Incorrect
The first three nodes are reversed, so the head becomes node 3, followed by 2 and 1. The code does not connect 1's next to 4, so printing from head shows 3 -> 2 -> 1 -> nullptr.
🧠 Conceptual
expertTime complexity of reversing a doubly linked list
What is the time complexity of reversing a doubly linked list with n nodes by swapping pointers in a single pass?
Attempts:
2 left
💡 Hint
Consider how many nodes are visited and how many operations per node.
✗ Incorrect
Reversing the list requires visiting each node once and swapping pointers, so the time complexity is linear, O(n).
