Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the given value.
DSA Go
newNode := &ListNode{Val: [1], Next: nil}Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name instead of a number.
Using incorrect field names.
✗ Incorrect
We create a new node with the value 5. The field name is Val, so we assign 5 to Val.
2fill in blank
mediumComplete the code to start merging from a dummy node.
DSA Go
dummy := &ListNode{}
current := [1]Attempts:
3 left
💡 Hint
Common Mistakes
Assigning current to nil or head instead of dummy.
Using an undefined variable.
✗ Incorrect
We start merging from the dummy node, so current points to dummy.
3fill in blank
hardFix the error in the condition to compare node values.
DSA Go
for l1 != nil && l2 != nil { if l1.Val [1] l2.Val { current.Next = l1 l1 = l1.Next } else { current.Next = l2 l2 = l2.Next } current = current.Next }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' which picks the larger value first.
Using '==' or '!=' which do not order nodes.
✗ Incorrect
We want to pick the smaller value node first, so we use '<' to compare.
4fill in blank
hardFill both blanks to attach the remaining nodes after the loop.
DSA Go
if l1 != nil { current.Next = [1] } else { current.Next = [2] }
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning current or dummy instead of l1 or l2.
Swapping l1 and l2 in the else block.
✗ Incorrect
After the loop, attach the remaining nodes from l1 or l2 to current.Next.
5fill in blank
hardFill all three blanks to return the merged list starting after dummy.
DSA Go
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
dummy := &ListNode{}
current := dummy
for l1 != nil && l2 != nil {
if l1.Val < l2.Val {
current.Next = l1
l1 = l1.Next
} else {
current.Next = l2
l2 = l2.Next
}
current = current.Next
}
if l1 != nil {
current.Next = l1
} else {
current.Next = l2
}
return [1]
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning dummy instead of dummy.Next.
Returning current or current.Next which may not be the head.
✗ Incorrect
We return dummy.Next because dummy is a placeholder node before the merged list.
