Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define the Node struct with a value and a pointer to the next node.
DSA Go
type Node struct {
value int
next [1]Node
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pointer' or 'ref' which are not valid Go syntax for pointers.
Using '&' which is the address operator, not a type declaration.
✗ Incorrect
In Go, to declare a pointer to a struct, we use the * symbol before the type name.
2fill in blank
mediumComplete the code to create a new node with a given value.
DSA Go
func NewNode(val int) *Node {
return &Node{value: [1]
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' which is the field name, not the parameter.
Using 'val.value' which is invalid since val is an int.
✗ Incorrect
The function parameter is named val, so we assign it to the value field of the Node struct.
3fill in blank
hardFix the error in the Append method to add a new node at the end of the list.
DSA Go
func (head *Node) Append(val int) {
current := head
for current.next != [1] {
current = current.next
}
current.next = NewNode(val)
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 0 or head instead of nil.
Using current instead of current.next in the loop condition.
✗ Incorrect
The loop should continue until current.next is nil, meaning the end of the list.
4fill in blank
hardFill both blanks to complete the PrintList method that prints all node values.
DSA Go
func (head *Node) PrintList() {
current := head
for current != [1] {
fmt.Print(current.[2], " -> ")
current = current.next
}
fmt.Println("nil")
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'val' instead of 'value' for the field name.
Checking for 'head' instead of 'nil' in the loop condition.
✗ Incorrect
The loop runs until current is nil, and the field to print is 'value'.
5fill in blank
hardFill all three blanks to complete the Remove method that deletes the first node with a given value.
DSA Go
func (head *Node) Remove(val int) *Node {
if head == [1] {
return head
}
if head.value == val {
return head.[2]
}
current := head
for current.next != [3] {
if current.next.value == val {
current.next = current.next.next
break
}
current = current.next
}
return head
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'head' instead of 'nil' for empty list check.
Returning head instead of head.next when removing the first node.
Looping until current.next equals head instead of nil.
✗ Incorrect
Check if head is nil, return head; if head.value matches val, return head.next; loop until current.next is nil.
