Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to remove the first node from the linked list.
DSA Go
func (list *LinkedList) Pop() *Node {
if list.Head == nil {
return nil
}
poppedNode := list.Head
list.Head = list.Head.[1]
return poppedNode
}Attempts:
3 left
💡 Hint
Common Mistakes
Using Prev instead of Next causes the list to move backward.
Using Child or Parent is incorrect as they are not linked list pointers.
✗ Incorrect
The Pop method removes the first node by moving the head pointer to the next node. So, list.Head.Next is assigned to list.Head.
2fill in blank
mediumComplete the code to check if the linked list is empty before popping.
DSA Go
func (list *LinkedList) Pop() *Node {
if list.Head == [1] {
return nil
}
poppedNode := list.Head
list.Head = list.Head.Next
return poppedNode
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking if list.Head == 0 is incorrect because head is a pointer, not a number.
Using true or false is incorrect as head is not a boolean.
✗ Incorrect
The linked list is empty if the head pointer is nil, so we check if list.Head == nil.
3fill in blank
hardFix the error in the Pop method to update the tail pointer when popping the last node.
DSA Go
func (list *LinkedList) Pop() *Node {
if list.Head == nil {
return nil
}
poppedNode := list.Head
list.Head = list.Head.Next
if list.Head == [1] {
list.Tail = [2]
}
return poppedNode
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting tail to poppedNode keeps a reference to removed node.
Setting tail to list.Head when head is nil causes errors.
✗ Incorrect
After popping, if the head becomes nil, it means the list is empty, so tail should also be set to nil.
4fill in blank
hardFill both blanks to correctly update the linked list after popping the first node.
DSA Go
func (list *LinkedList) Pop() *Node {
if list.Head == nil {
return nil
}
poppedNode := list.Head
list.Head = list.Head.[1]
if list.Head == nil {
list.Tail = [2]
}
return poppedNode
}Attempts:
3 left
💡 Hint
Common Mistakes
Using Prev instead of Next moves backward incorrectly.
Setting tail to poppedNode keeps removed node reference.
✗ Incorrect
We move head to head.Next and if head is nil, tail must be set to nil to indicate empty list.
5fill in blank
hardFill all three blanks to safely pop the first node and update pointers.
DSA Go
func (list *LinkedList) Pop() *Node {
if list.Head == [1] {
return nil
}
poppedNode := list.Head
list.Head = list.Head.[2]
if list.Head == [3] {
list.Tail = nil
}
return poppedNode
}Attempts:
3 left
💡 Hint
Common Mistakes
Not checking for nil head causes runtime errors.
Using poppedNode instead of nil when updating tail.
Using list.Head instead of nil in condition.
✗ Incorrect
Check if head is nil before popping, move head to head.Next, and if head is nil after popping, set tail to nil.
