Challenge - 5 Problems
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of reversing a doubly linked list
What is the printed state of the doubly linked list after reversing it?
DSA C
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
Node* reverse(Node* head) {
Node* temp = NULL;
Node* current = head;
while (current != NULL) {
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}
if (temp != NULL) {
head = temp->prev;
}
return head;
}
void printList(Node* node) {
while (node != NULL) {
printf("%d", node->data);
if (node->next != NULL) printf(" <-> ");
node = node->next;
}
printf("\n");
}
int main() {
Node* head = malloc(sizeof(Node));
Node* second = malloc(sizeof(Node));
Node* third = malloc(sizeof(Node));
head->data = 1; head->prev = NULL; head->next = second;
second->data = 2; second->prev = head; second->next = third;
third->data = 3; third->prev = second; third->next = NULL;
head = reverse(head);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Think about how the prev and next pointers are swapped in each node.
✗ Incorrect
The reverse function swaps the prev and next pointers for each node. After the loop, the head pointer is updated to the last node processed, which becomes the new head. The printed list shows nodes in reverse order with <-> indicating doubly linked connections.
🧠 Conceptual
intermediate1:30remaining
Understanding pointer swaps in reversing a doubly linked list
In the reversal of a doubly linked list, what is the main reason for swapping the prev and next pointers of each node?
Attempts:
2 left
💡 Hint
Think about how links define the order of nodes.
✗ Incorrect
Swapping prev and next pointers reverses the direction of the links between nodes, so the list can be traversed in the opposite order.
🔧 Debug
advanced2:00remaining
Identify the error in this reverse function for doubly linked list
What error will this reverse function cause when reversing a doubly linked list?
DSA C
Node* reverse(Node* head) {
Node* temp = NULL;
Node* current = head;
while (current != NULL) {
temp = current->next;
current->next = current->prev;
current->prev = temp;
current = current->next;
}
if (temp != NULL) {
head = temp->prev;
}
return head;
}Attempts:
2 left
💡 Hint
Check how current pointer moves after swapping.
✗ Incorrect
The current pointer moves to current->next, but after swapping, current->next points to the previous node, causing the loop to move backward and never reach NULL, resulting in an infinite loop.
❓ Predict Output
advanced2:00remaining
Output after reversing a doubly linked list with 4 nodes
Given a doubly linked list with nodes 10 <-> 20 <-> 30 <-> 40, what is the printed output after applying the reverse function below?
DSA C
Node* reverse(Node* head) {
Node* temp = NULL;
Node* current = head;
while (current != NULL) {
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}
if (temp != NULL) {
head = temp->prev;
}
return head;
}
// After reversal, printList prints nodes connected by <->Attempts:
2 left
💡 Hint
The reverse function swaps prev and next pointers for each node.
✗ Incorrect
The reverse function swaps the prev and next pointers for each node, effectively reversing the list order. The printList function prints nodes connected by <->, so the reversed list prints 40 <-> 30 <-> 20 <-> 10.
🧠 Conceptual
expert1:30remaining
Why is updating the head pointer after reversal necessary?
After reversing a doubly linked list by swapping prev and next pointers, why must we update the head pointer to the last processed node?
Attempts:
2 left
💡 Hint
Think about the position of the head before and after reversal.
✗ Incorrect
The original head node becomes the tail after reversal. The new head is the last node processed in the loop, so we must update the head pointer to point to it to correctly represent the reversed list.
