Challenge - 5 Problems
Doubly Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Doubly Linked List after Insertions
What is the printed state of the doubly linked list after inserting nodes with values 10, 20, and 30 at the end?
DSA C++
struct Node {
int data;
Node* prev;
Node* next;
Node(int val) : data(val), prev(nullptr), next(nullptr) {}
};
class DoublyLinkedList {
public:
Node* head;
Node* tail;
DoublyLinkedList() : head(nullptr), tail(nullptr) {}
void insertEnd(int val) {
Node* newNode = new Node(val);
if (!head) {
head = tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}
void printList() {
Node* temp = head;
while (temp) {
std::cout << temp->data;
if (temp->next) std::cout << " <-> ";
temp = temp->next;
}
std::cout << " -> null" << std::endl;
}
};
int main() {
DoublyLinkedList dll;
dll.insertEnd(10);
dll.insertEnd(20);
dll.insertEnd(30);
dll.printList();
return 0;
}Attempts:
2 left
💡 Hint
Remember that insertEnd adds nodes at the tail, so the order is the same as insertion.
✗ Incorrect
Nodes are inserted at the end, so the list prints 10, then 20, then 30 connected by <-> arrows, ending with -> null.
🧠 Conceptual
intermediateUnderstanding Node Pointers in Doubly Linked List
In a doubly linked list node, which pointers are used to navigate the list?
Attempts:
2 left
💡 Hint
Think about how you can move forward and backward in the list.
✗ Incorrect
A doubly linked list node has two pointers: one to the next node and one to the previous node, allowing traversal in both directions.
🔧 Debug
advancedIdentify the Bug in Node Deletion Code
What error will occur when deleting the head node in this doubly linked list code snippet?
DSA C++
void deleteHead() {
if (!head) return;
Node* temp = head;
head = head->next;
head->prev = nullptr;
delete temp;
}Attempts:
2 left
💡 Hint
What happens if the list has only one node?
✗ Incorrect
If the list has one node, after head = head->next, head becomes nullptr. Then head->prev causes a runtime error (nullptr dereference).
❓ Predict Output
advancedResult of Reversing a Doubly Linked List
What is the printed state of the doubly linked list after reversing it?
DSA C++
void reverseList(Node*& head) {
Node* temp = nullptr;
Node* current = head;
while (current) {
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}
if (temp) head = temp->prev;
}
int main() {
DoublyLinkedList dll;
dll.insertEnd(1);
dll.insertEnd(2);
dll.insertEnd(3);
reverseList(dll.head);
dll.printList();
return 0;
}Attempts:
2 left
💡 Hint
Reversing swaps next and prev pointers for each node and updates head.
✗ Incorrect
After reversing, the list order is reversed, so it prints 3 <-> 2 <-> 1 -> null.
🧠 Conceptual
expertMemory Management in Doubly Linked List Node Deletion
When deleting a node from a doubly linked list, which step is crucial to avoid memory leaks and dangling pointers?
Attempts:
2 left
💡 Hint
Think about how the list stays connected after removal.
✗ Incorrect
To safely remove a node, pointers of adjacent nodes must be updated to bypass the node, then the node memory is freed.
