Challenge - 5 Problems
Doubly Linked List Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting at position 3 in a doubly linked list
What is the printed state of the doubly linked list after inserting the value 25 at position 3?
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 append(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 insertAtPosition(int val, int pos) {
Node* newNode = new Node(val);
if (pos == 1) {
newNode->next = head;
if (head) head->prev = newNode;
head = newNode;
return;
}
Node* temp = head;
int count = 1;
while (temp && count < pos - 1) {
temp = temp->next;
count++;
}
if (!temp) return; // position out of bounds
newNode->next = temp->next;
if (temp->next) temp->next->prev = newNode;
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.append(10);
dll.append(20);
dll.append(30);
dll.append(40);
dll.insertAtPosition(25, 3);
dll.printList();
return 0;
}Attempts:
2 left
💡 Hint
Remember that position 3 means the new node should be the third node in the list.
✗ Incorrect
The list initially is 10 <-> 20 <-> 30 <-> 40 <-> null. Inserting 25 at position 3 places it between 20 and 30.
❓ Predict Output
intermediateOutput after inserting at position 1 in a doubly linked list
What is the printed state of the doubly linked list after inserting the value 5 at position 1?
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 append(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 insertAtPosition(int val, int pos) {
Node* newNode = new Node(val);
if (pos == 1) {
newNode->next = head;
if (head) head->prev = newNode;
head = newNode;
return;
}
Node* temp = head;
int count = 1;
while (temp && count < pos - 1) {
temp = temp->next;
count++;
}
if (!temp) return; // position out of bounds
newNode->next = temp->next;
if (temp->next) temp->next->prev = newNode;
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.append(10);
dll.append(20);
dll.append(30);
dll.insertAtPosition(5, 1);
dll.printList();
return 0;
}Attempts:
2 left
💡 Hint
Inserting at position 1 means the new node becomes the new head.
✗ Incorrect
The new node with value 5 is inserted before the current head (10), becoming the new head.
🔧 Debug
advancedIdentify the error in insertAtPosition method
What error will occur when trying to insert at position 5 in a list of 3 nodes using this insertAtPosition method?
DSA C++
void insertAtPosition(int val, int pos) { Node* newNode = new Node(val); if (pos == 1) { newNode->next = head; if (head) head->prev = newNode; head = newNode; return; } Node* temp = head; int count = 1; while (temp && count < pos - 1) { temp = temp->next; count++; } if (!temp) return; // Added check to prevent nullptr dereference newNode->next = temp->next; if (temp->next) temp->next->prev = newNode; temp->next = newNode; newNode->prev = temp; }
Attempts:
2 left
💡 Hint
Consider what happens if pos is greater than the length + 1 of the list.
✗ Incorrect
If pos is beyond the list length + 1, temp becomes nullptr and accessing temp->next causes a segmentation fault.
🧠 Conceptual
advancedEffect of inserting at position beyond list length + 1
What happens if you try to insert a node at position 10 in a doubly linked list that currently has 5 nodes, using the insertAtPosition method shown?
Attempts:
2 left
💡 Hint
Look at how the method handles the case when temp becomes nullptr.
✗ Incorrect
The method returns early if temp is nullptr, so no insertion happens and the list stays the same.
🚀 Application
expertFinal list after multiple insertions at various positions
Given an initially empty doubly linked list, after performing these operations in order:
1. append(10)
2. append(20)
3. insertAtPosition(15, 2)
4. insertAtPosition(5, 1)
5. insertAtPosition(25, 5)
What is the printed state of the 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;
DoublyLinkedList() : head(nullptr) {}
void append(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 insertAtPosition(int val, int pos) {
Node* newNode = new Node(val);
if (pos == 1) {
newNode->next = head;
if (head) head->prev = newNode;
head = newNode;
return;
}
Node* temp = head;
int count = 1;
while (temp && count < pos - 1) {
temp = temp->next;
count++;
}
if (!temp) return;
newNode->next = temp->next;
if (temp->next) temp->next->prev = newNode;
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.append(10);
dll.append(20);
dll.insertAtPosition(15, 2);
dll.insertAtPosition(5, 1);
dll.insertAtPosition(25, 5);
dll.printList();
return 0;
}Attempts:
2 left
💡 Hint
Track each insertion carefully and remember the list positions shift after each insertion.
✗ Incorrect
After all operations, the list is: 5 (pos 1), 10 (pos 2), 15 (pos 3), 20 (pos 4), 25 (pos 5).
