Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to remove the top node from the linked list stack.
DSA C++
Node* pop(Node*& head) {
if (head == nullptr) return nullptr;
Node* temp = head;
head = head->[1];
return temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' causes errors because 'prev' is not defined.
Using 'child' or 'link' which are not valid pointers in this node structure.
✗ Incorrect
The 'next' pointer points to the next node in the linked list. To pop the top node, we move head to head->next.
2fill in blank
mediumComplete the code to check if the linked list stack is empty before popping.
DSA C++
bool isEmpty(Node* head) {
return head == [1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or NULL instead of nullptr in modern C++.
Comparing head to itself which is always true.
✗ Incorrect
The head pointer is nullptr when the linked list is empty.
3fill in blank
hardFix the error in the pop function to properly delete the popped node.
DSA C++
Node* pop(Node*& head) {
if (head == nullptr) return nullptr;
Node* temp = head;
head = head->next;
[1] temp;
return nullptr;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'free' instead of 'delete' in C++.
Not deleting the node causing memory leaks.
✗ Incorrect
In C++, 'delete' is used to free memory allocated with 'new'.
4fill in blank
hardFill both blanks to pop the top node and return its value.
DSA C++
int popValue(Node*& head) {
if (head == nullptr) return -1;
Node* temp = head;
int val = temp->[1];
head = head->[2];
delete temp;
return val;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' instead of 'data' for the node's stored value.
Using 'prev' instead of 'next' to move head forward.
✗ Incorrect
The node's stored value is in 'data', and the next node is pointed to by 'next'.
5fill in blank
hardFill all three blanks to pop the top node, update head, and safely return the popped node's data.
DSA C++
int popSafe(Node*& head) {
if (head == nullptr) return -1;
Node* temp = head;
int result = temp->[1];
head = head->[2];
[3] temp;
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'free' instead of 'delete' to free memory.
Returning the node pointer instead of its data value.
✗ Incorrect
We access the node's 'data', move head to 'next', and delete the popped node with 'delete'.
