Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to delete the head node from the linked list.
DSA C++
void deleteHead(Node*& head) {
if (head == nullptr) return;
Node* temp = head;
head = head->[1];
delete temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using head->prev instead of head->next.
Deleting head without moving the pointer.
Assigning head to itself.
✗ Incorrect
To delete the head node, we move the head pointer to the next node using head->next.
2fill in blank
mediumComplete the code to delete the node at position pos (1-based index) in the linked list.
DSA C++
void deleteAtPosition(Node*& head, int pos) {
if (pos == 1) {
deleteHead(head);
return;
}
Node* current = head;
for (int i = 1; i < pos - 1 && current != nullptr; i++) {
current = current->[1];
}
if (current == nullptr || current->next == nullptr) return;
Node* temp = current->next;
current->next = temp->next;
delete temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using current->prev which is invalid in singly linked list.
Not checking if current is nullptr.
Deleting the wrong node.
✗ Incorrect
To reach the node before the one to delete, we move current using current->next.
3fill in blank
hardFix the error in the code to correctly delete the last node of the linked list.
DSA C++
void deleteLast(Node*& head) {
if (head == nullptr) return;
if (head->next == nullptr) {
delete head;
head = nullptr;
return;
}
Node* current = head;
while (current->[1]->next != nullptr) {
current = current->next;
}
delete current->next;
current->next = nullptr;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using current->prev which does not exist in singly linked list.
Deleting current instead of current->next.
Not updating current->next to nullptr after deletion.
✗ Incorrect
To find the second last node, we move current using current->next until current->next->next is nullptr.
4fill in blank
hardFill both blanks to delete the node at position pos in a singly linked list.
DSA C++
void deleteAtPosition(Node*& head, int pos) {
if (pos == 1) {
Node* temp = head;
head = head->[1];
delete temp;
return;
}
Node* current = head;
for (int i = 1; i < pos - 1 && current != nullptr; i++) {
current = current->[2];
}
if (current == nullptr || current->next == nullptr) return;
Node* temp = current->next;
current->next = temp->next;
delete temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' pointer which does not exist in singly linked list.
Not updating head correctly when deleting the first node.
Not checking for nullptr before accessing pointers.
✗ Incorrect
Both blanks require moving to the next node using the 'next' pointer to traverse and update links correctly.
5fill in blank
hardFill all three blanks to delete the node at position pos safely, including edge cases.
DSA C++
void deleteAtPosition(Node*& head, int pos) {
if (head == nullptr) return;
if (pos == 1) {
Node* temp = head;
head = head->[1];
delete temp;
return;
}
Node* current = head;
for (int i = 1; i < pos - 1 && current != nullptr; i++) {
current = current->[2];
}
if (current == nullptr || current->next == nullptr) return;
Node* temp = current->next;
current->next = temp->[3];
delete temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' pointer which does not exist.
Not updating the head pointer when deleting the first node.
Not checking for nullptr before accessing pointers.
✗ Incorrect
All blanks require the 'next' pointer to move forward and update links correctly in the singly linked list.
