Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a linked list node struct in Go.
DSA Go
type Node struct {
value int
next [1]
}Attempts:
3 left
💡 Hint
Common Mistakes
Using Node instead of *Node causes infinite size error.
Using int or string for next is incorrect.
✗ Incorrect
In Go, a linked list node points to the next node using a pointer to Node, written as *Node.
2fill in blank
mediumComplete the code to add a new node at the beginning of a linked list.
DSA Go
func prepend(head *Node, val int) *Node {
newNode := &Node{value: val, next: [1]
return newNode
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting next to nil loses the existing list.
Using &head or *head is incorrect pointer usage.
✗ Incorrect
The new node's next should point to the current head to insert at the beginning.
3fill in blank
hardFix the error in the code to traverse a linked list and print values.
DSA Go
func printList(head *Node) {
current := head
for current != [1] {
fmt.Println(current.value)
current = current.next
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using head or current as condition causes infinite loop.
Using next is undefined in this scope.
✗ Incorrect
The loop should continue until current is nil, meaning end of list.
4fill in blank
hardFill both blanks to create a linked list with three nodes and link them.
DSA Go
node1 := &Node{value: 1, next: [1]
node2 := &Node{value: 2, next: [2]
node3 := &Node{value: 3, next: nil}Attempts:
3 left
💡 Hint
Common Mistakes
Using &node3 is incorrect because node3 is already a pointer.
Setting node2.next to nil leaves node3 unlinked.
✗ Incorrect
node1.next points to node2, node2.next points to node3 to link all three nodes, with node3.next already nil.
5fill in blank
hardFill all three blanks to insert a new node with value 4 after node2.
DSA Go
newNode := &Node{value: 4, next: [1]
node2.next = [2]
head := [3]Attempts:
3 left
💡 Hint
Common Mistakes
Setting newNode.next to node3 directly breaks list if node2.next was different.
Assigning head incorrectly changes list start.
✗ Incorrect
newNode.next points to node2.next, node2.next updated to newNode, head remains node1.
