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#
public int Pop()
{
if (head == null) throw new InvalidOperationException("Stack is empty");
int value = head.Value;
head = head.[1];
return value;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Previous' instead of 'Next' causes errors because singly linked lists don't have previous pointers.
Using 'Child' or 'Parent' are invalid properties for linked list nodes.
✗ Incorrect
The 'Next' pointer moves the head to the next node, effectively removing the top node.
2fill in blank
mediumComplete the code to check if the stack is empty before popping.
DSA C#
public int Pop()
{
if (head == [1]) throw new InvalidOperationException("Stack is empty");
int value = head.Value;
head = head.Next;
return value;
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking head == 0 is incorrect because head is a node reference, not a number.
Using true or false is invalid for node references.
✗ Incorrect
The head being null means the stack is empty.
3fill in blank
hardFix the error in the Pop method to correctly update the head pointer.
DSA C#
public int Pop()
{
if (head == null) throw new InvalidOperationException("Stack is empty");
int value = head.Value;
head = [1];
return value;
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning head to head.Value causes a type error.
Assigning head to null removes all nodes, not just the top.
✗ Incorrect
Assigning head to head.Next moves the head to the next node, removing the top node.
4fill in blank
hardFill both blanks to complete the Pop method that returns the popped value and updates the head.
DSA C#
public int Pop()
{
if (head == [1]) throw new InvalidOperationException("Stack is empty");
int value = head.[2];
head = head.Next;
return value;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using head.Next in the if condition is wrong because it doesn't check if stack is empty.
Using head.Next instead of head.Value to get the value is incorrect.
✗ Incorrect
Check if head is null to detect empty stack, then get the value from head.Value.
5fill in blank
hardFill all three blanks to implement Pop that checks empty, saves value, updates head, and returns value.
DSA C#
public int Pop()
{
if (head == [1]) throw new InvalidOperationException("Stack is empty");
int value = head.[2];
head = head.[3];
return value;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using head.Previous instead of head.Next to update head.
Getting value from head.Next instead of head.Value.
✗ Incorrect
Check if head is null, get value from head.Value, then move head to head.Next.
