Challenge - 5 Problems
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after inserting 10 at the beginning?
Consider a doubly linked list initially containing nodes with values 20 -> 30 -> 40 -> null. We insert a new node with value 10 at the beginning. What is the printed list after this operation?
DSA C++
struct Node {
int data;
Node* prev;
Node* next;
};
void insertAtBeginning(Node*& head, int val) {
Node* newNode = new Node();
newNode->data = val;
newNode->prev = nullptr;
newNode->next = head;
if (head != nullptr) {
head->prev = newNode;
}
head = newNode;
}
void printList(Node* head) {
while (head != nullptr) {
std::cout << head->data << " -> ";
head = head->next;
}
std::cout << "null" << std::endl;
}
int main() {
Node* head = new Node{20, nullptr, nullptr};
head->next = new Node{30, head, nullptr};
head->next->next = new Node{40, head->next, nullptr};
insertAtBeginning(head, 10);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Remember, inserting at the beginning means the new node becomes the head and points to the old head.
✗ Incorrect
The new node with value 10 is inserted before the current head (20). So the list becomes 10 -> 20 -> 30 -> 40 -> null.
❓ Predict Output
intermediateWhat is the output after inserting 5 at the beginning of an empty list?
Given an empty doubly linked list (head is nullptr), we insert a node with value 5 at the beginning. What is the printed list after this operation?
DSA C++
struct Node {
int data;
Node* prev;
Node* next;
};
void insertAtBeginning(Node*& head, int val) {
Node* newNode = new Node();
newNode->data = val;
newNode->prev = nullptr;
newNode->next = head;
if (head != nullptr) {
head->prev = newNode;
}
head = newNode;
}
void printList(Node* head) {
while (head != nullptr) {
std::cout << head->data << " -> ";
head = head->next;
}
std::cout << "null" << std::endl;
}
int main() {
Node* head = nullptr;
insertAtBeginning(head, 5);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Inserting into an empty list creates a single node which is the head.
✗ Incorrect
Since the list was empty, the new node with value 5 becomes the only node and head of the list.
🔧 Debug
advancedIdentify the bug in this insertAtBeginning function
The following code attempts to insert a node at the beginning of a doubly linked list. What is the bug that will cause incorrect behavior?
DSA C++
void insertAtBeginning(Node*& head, int val) { Node* newNode = new Node(); newNode->data = val; newNode->prev = head; newNode->next = nullptr; if (head != nullptr) { head->prev = newNode; } head = newNode; }
Attempts:
2 left
💡 Hint
The previous pointer of the new head node should point to nothing before it.
✗ Incorrect
When inserting at the beginning, the new node's prev pointer must be nullptr because it becomes the first node. Setting it to head is wrong.
❓ Predict Output
advancedWhat is the output after multiple insertions at beginning?
Starting with an empty doubly linked list, we insert 3, then 2, then 1 at the beginning in that order. What is the printed list after these operations?
DSA C++
struct Node {
int data;
Node* prev;
Node* next;
};
void insertAtBeginning(Node*& head, int val) {
Node* newNode = new Node();
newNode->data = val;
newNode->prev = nullptr;
newNode->next = head;
if (head != nullptr) {
head->prev = newNode;
}
head = newNode;
}
void printList(Node* head) {
while (head != nullptr) {
std::cout << head->data << " -> ";
head = head->next;
}
std::cout << "null" << std::endl;
}
int main() {
Node* head = nullptr;
insertAtBeginning(head, 3);
insertAtBeginning(head, 2);
insertAtBeginning(head, 1);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Each new insertion at beginning becomes the new head.
✗ Incorrect
Inserting 3 first makes list 3 -> null, then 2 at beginning makes 2 -> 3 -> null, then 1 at beginning makes 1 -> 2 -> 3 -> null.
🧠 Conceptual
expertWhy must the previous pointer of the old head be updated when inserting at beginning?
When inserting a new node at the beginning of a doubly linked list, why is it necessary to update the previous pointer of the old head node to point to the new node?
Attempts:
2 left
💡 Hint
Think about how doubly linked lists allow moving backwards.
✗ Incorrect
The previous pointer of the old head must point to the new node to keep the backward links intact, enabling traversal from tail to head.
