Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to delete the first node of a singly linked list.
DSA C++
void deleteBeginning(Node*& head) {
if (head == nullptr) return;
Node* temp = head;
head = head->[1];
delete temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using head->prev which is not valid in singly linked list.
Deleting head without moving it to next node.
✗ Incorrect
To delete the first node, we move the head pointer to the next node using head->next.
2fill in blank
mediumComplete the code to safely delete the first node and avoid memory leaks.
DSA C++
void deleteBeginning(Node*& head) {
if (head == nullptr) return;
Node* temp = head;
head = head->next;
[1] temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using free() instead of delete causing undefined behavior.
Not deleting the node causing memory leaks.
✗ Incorrect
In C++, 'delete' is used to free memory allocated with 'new'.
3fill in blank
hardFix the error in the code that deletes the first node but causes a crash.
DSA C++
void deleteBeginning(Node*& head) {
if (head == nullptr) return;
Node* temp = head;
head = [1];
delete temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using head->prev which is invalid in singly linked list.
Using head->next before assigning temp causing dangling pointer.
✗ Incorrect
We must move head to the next node of temp before deleting temp.
4fill in blank
hardFill both blanks to delete the first node and update the head pointer correctly.
DSA C++
void deleteBeginning(Node*& head) {
if (head == nullptr) return;
Node* temp = [1];
head = [2];
delete temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning temp to nullptr instead of head.
Not updating head to head->next before deleting temp.
✗ Incorrect
temp stores the current head, head moves to next node before deleting temp.
5fill in blank
hardFill all three blanks to delete the first node and handle empty list safely.
DSA C++
void deleteBeginning(Node*& head) {
if (head == [1]) return;
Node* temp = [2];
head = [3];
delete temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Not checking for empty list causing crashes.
Deleting head without moving it to next node.
✗ Incorrect
Check if head is nullptr, temp stores head, head moves to next node before deleting temp.
