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 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
The insertEnd method adds nodes at the end, so the list prints in the order 10, 20, 30 connected with <-> arrows, ending with -> null.
❓ Predict Output
intermediateState of Doubly Linked List after Inserting at Front
What is the printed state of the doubly linked list after inserting nodes with values 5, 15, and 25 at the front?
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 insertFront(int val) {
Node* newNode = new Node(val);
if (!head) {
head = tail = newNode;
} else {
newNode->next = head;
head->prev = newNode;
head = 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.insertFront(5);
dll.insertFront(15);
dll.insertFront(25);
dll.printList();
return 0;
}Attempts:
2 left
💡 Hint
Inserting at front means new nodes become the new head, pushing older nodes back.
✗ Incorrect
Each insertFront adds a new node before the current head, so the list order is 25, 15, 5 connected with <-> arrows, ending with -> null.
🔧 Debug
advancedIdentify the Bug in Doubly Linked List Initialization
What error will this code produce when trying to insert a node at the end of the doubly linked list?
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;
}
}
};
int main() {
DoublyLinkedList dll;
dll.insertEnd(10);
return 0;
}Attempts:
2 left
💡 Hint
Check if all pointers are properly initialized before use.
✗ Incorrect
The tail pointer was not initialized in the constructor, which could cause runtime errors. Initializing tail to nullptr fixes this issue.
❓ Predict Output
advancedOutput after Deleting a Node from Doubly Linked List
What is the printed state of the doubly linked list after deleting the node with value 20?
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 deleteNode(int val) {
Node* temp = head;
while (temp && temp->data != val) {
temp = temp->next;
}
if (!temp) return;
if (temp->prev) temp->prev->next = temp->next;
else head = temp->next;
if (temp->next) temp->next->prev = temp->prev;
else tail = temp->prev;
delete temp;
}
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.deleteNode(20);
dll.printList();
return 0;
}Attempts:
2 left
💡 Hint
Deleting a node removes it and connects its previous and next nodes directly.
✗ Incorrect
Node with value 20 is removed, so the list links 10 directly to 30 with <-> arrows, ending with -> null.
🧠 Conceptual
expertMemory Management in Doubly Linked List
Which statement best describes the correct way to avoid memory leaks when deleting nodes in a doubly linked list implemented in C++?
Attempts:
2 left
💡 Hint
Think about what happens if you remove a node but never free its memory.
✗ Incorrect
In C++, after unlinking a node from the list, you must explicitly delete it to free memory and avoid leaks.
