Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to move to the last node in the linked list.
DSA Go
for current := head; current != nil && current.[1] != nil; current = current.next { // loop to find last node }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' to move forward.
Trying to access 'value' to move to next node.
✗ Incorrect
We use current.next to move to the next node until we reach the last node where next is nil.
2fill in blank
mediumComplete the code to update the second last node's next pointer to nil, effectively deleting the last node.
DSA Go
if current != nil && current.[1] != nil { current.[2] = nil }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting 'prev' to nil instead of 'next'.
Trying to set 'value' to nil which is invalid.
✗ Incorrect
We check if current.next is not nil, then set current.next to nil to remove the last node.
3fill in blank
hardFix the error in the code to handle the case when the list has only one node.
DSA Go
if head != nil && head.[1] == nil { head = nil }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking head.prev which does not exist in singly linked list.
Checking head.value instead of head.next.
✗ Incorrect
If head.next is nil, it means there is only one node, so setting head to nil deletes it.
4fill in blank
hardFill both blanks to traverse the list and keep track of the previous node.
DSA Go
prev := head current := head.[1] for current != nil && current.[2] != nil { prev = current current = current.next }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' to move current forward.
Using 'value' instead of 'next' to traverse.
✗ Incorrect
We start current at head.next and move forward using current.next while updating prev.
5fill in blank
hardFill all three blanks to delete the last node and return the updated head.
DSA Go
func deleteEnd(head *Node) *Node {
if head == nil {
return nil
}
if head.[1] == nil {
return nil
}
prev := head
current := head.[2]
for current.[3] != nil {
prev = current
current = current.next
}
prev.next = nil
return head
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' in conditions.
Not handling the case when list has only one node.
✗ Incorrect
We check if head.next is nil, start current at head.next, and loop while current.next is not nil to find last node.
