Complete the code to declare an array of integers with 5 elements.
var numbers = [[1]]int{}The code declares an array of 5 integers. The number inside the brackets specifies the size.
Complete the code to create a linked list node struct with an integer value and a pointer to the next node.
type Node struct {
value int
[1] *Node
}The field 'next' points to the next node in the linked list.
Fix the error in the code to append a new node to the end of a singly linked list.
func appendNode(head *Node, val int) *Node {
newNode := &Node{value: val}
if head == nil {
return newNode
}
current := head
for current.[1] != nil {
current = current.next
}
current.next = newNode
return head
}The loop uses 'current.next' to traverse nodes until the end.
Fill both blanks to create a function that returns the length of a linked list.
func length(head *Node) int {
count := 0
current := head
for current != [1] {
count[2]1
current = current.next
}
return count
}The loop runs until current is nil, counting nodes by incrementing count with '+='.
Fill all three blanks to create a function that inserts a new node at the beginning of a linked list.
func insertAtHead(head *Node, val int) *Node {
newNode := &Node{value: [1]
newNode.[2] = [3]
return newNode
}The new node's value is set to val, its next points to the current head, and then it becomes the new head.
