Challenge - 5 Problems
Head Insert Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after inserting nodes at the beginning?
Consider the following C++ code that inserts nodes at the beginning of a singly linked list. What will be the printed list after all insertions?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
void insertAtBeginning(Node*& head, int val) {
Node* newNode = new Node(val);
newNode->next = head;
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, 10);
insertAtBeginning(head, 20);
insertAtBeginning(head, 30);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Remember that inserting at the beginning means the new node becomes the new head.
✗ Incorrect
Each new node is inserted at the start, so the last inserted node appears first in the list.
🧠 Conceptual
intermediateWhat happens to the old head when a new node is inserted at the beginning?
When inserting a new node at the beginning of a singly linked list, what happens to the previous head node?
Attempts:
2 left
💡 Hint
Think about how the new node's next pointer is set.
✗ Incorrect
The new node points to the old head, so the old head becomes the second node.
🔧 Debug
advancedIdentify the error in this head insertion code
What error will this code cause when inserting at the beginning of a linked list?
DSA C++
struct Node {
int data;
Node* next;
};
void insertAtBeginning(Node* head, int val) {
Node* newNode = new Node();
newNode->data = val;
newNode->next = head;
head = newNode;
}
int main() {
Node* head = nullptr;
insertAtBeginning(head, 5);
insertAtBeginning(head, 10);
// printList(head) assumed
return 0;
}Attempts:
2 left
💡 Hint
Check how the head pointer is passed to the function.
✗ Incorrect
The head pointer is passed by value, so changes inside the function do not affect the original head.
❓ Predict Output
advancedOutput after multiple head insertions and one deletion
Given the code below, what is the output after all operations?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
void insertAtBeginning(Node*& head, int val) {
Node* newNode = new Node(val);
newNode->next = head;
head = newNode;
}
void deleteHead(Node*& head) {
if (head == nullptr) return;
Node* temp = head;
head = head->next;
delete temp;
}
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, 1);
insertAtBeginning(head, 2);
insertAtBeginning(head, 3);
deleteHead(head);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Deleting the head removes the first inserted node.
✗ Incorrect
After inserting 3,2,1 at head, list is 3->2->1. Deleting head removes 3, leaving 2->1.
🧠 Conceptual
expertWhy is passing head by reference important in head insertion?
In C++, why must the head pointer be passed by reference (Node*&) when inserting at the beginning of a linked list?
Attempts:
2 left
💡 Hint
Think about how pointers and references work in C++ function parameters.
✗ Incorrect
Passing by reference allows the function to change the caller's head pointer to the new node.
