Challenge - 5 Problems
Doubly Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting nodes at the end
What is the printed state of the doubly linked list after inserting 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;
DoublyLinkedList() : head(nullptr) {}
void insertAtEnd(int val) {
Node* newNode = new Node(val);
if (!head) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = 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.insertAtEnd(10);
dll.insertAtEnd(20);
dll.insertAtEnd(30);
dll.printList();
return 0;
}Attempts:
2 left
💡 Hint
Remember that inserting at the end adds nodes after the last node, preserving order.
✗ Incorrect
The insertAtEnd function adds nodes at the end, so the list maintains insertion order: 10, then 20, then 30. The printList function prints nodes from head to tail with arrows.
🧠 Conceptual
intermediateUnderstanding pointer updates in insertion at end
When inserting a new node at the end of a doubly linked list, which pointers must be updated to maintain list integrity?
Attempts:
2 left
💡 Hint
Think about how the new node connects to the existing last node.
✗ Incorrect
To insert at the end, the new node's prev pointer must point to the current last node, and the last node's next pointer must point to the new node. This keeps the chain intact.
🔧 Debug
advancedIdentify the bug in this insertion code
What error will occur when running this code snippet to insert at the end of a doubly linked list?
DSA C++
void insertAtEnd(int val) { Node* newNode = new Node(val); if (!head) { head = newNode; return; } Node* temp = head; while (temp->next) { temp = temp->next; } temp->next = newNode; // Missing line here }
Attempts:
2 left
💡 Hint
Check if all pointers of the new node are properly assigned.
✗ Incorrect
The code sets temp->next to newNode but forgets to set newNode->prev to temp. This breaks backward links in the doubly linked list.
❓ Predict Output
advancedOutput after inserting into an empty list
What is the output after inserting a single node with value 5 into an empty doubly linked list and printing it?
DSA C++
struct Node {
int data;
Node* prev;
Node* next;
Node(int val) : data(val), prev(nullptr), next(nullptr) {}
};
class DoublyLinkedList {
public:
Node* head;
DoublyLinkedList() : head(nullptr) {}
void insertAtEnd(int val) {
Node* newNode = new Node(val);
if (!head) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = 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.insertAtEnd(5);
dll.printList();
return 0;
}Attempts:
2 left
💡 Hint
A single node has no next node, so no backward or forward arrows except the end.
✗ Incorrect
The list has one node with data 5. The printList prints the data and then '-> null' since next is null. No '<->' appears because there is no next node.
🧠 Conceptual
expertTime complexity of inserting at the end with and without tail pointer
What is the time complexity of inserting a node at the end of a doubly linked list if we do NOT maintain a tail pointer, and how does it change if we DO maintain a tail pointer?
Attempts:
2 left
💡 Hint
Think about how many nodes you must traverse to find the end.
✗ Incorrect
Without a tail pointer, you must traverse the entire list to find the last node, which takes O(n) time. With a tail pointer, you can access the last node directly, so insertion is O(1).
