Hint: New node's next = old head; head = new node [OK]
Common Mistakes:
Not linking new node to old head
Changing head.next instead of head
Forgetting to update head pointer
3. What will be the output linked list after executing this Go code snippet?
package main
import "fmt"
type Node struct {
data int
next *Node
}
func main() {
head := &Node{data: 5, next: nil}
newNode := &Node{data: 3, next: head}
head = newNode
newNode2 := &Node{data: 1, next: head}
head = newNode2
// Print list
current := head
for current != nil {
fmt.Print(current.data, " -> ")
current = current.next
}
fmt.Print("null")
}
medium
A. 1 -> 3 -> 5 -> null
B. 5 -> 3 -> 1 -> null
C. 3 -> 1 -> 5 -> null
D. null
Solution
Step 1: Insert 3 at beginning
New node with 3 points to 5, head updated to 3.
Step 2: Insert 1 at beginning
New node with 1 points to 3, head updated to 1.
Step 3: Print list from head
Traverse from head: 1 -> 3 -> 5 -> null.
Final Answer:
1 -> 3 -> 5 -> null -> Option A
Quick Check:
Head insert order reversed list [OK]
Hint: New nodes become head, so list order reverses insert order [OK]
Common Mistakes:
Printing in insertion order instead of head order
Confusing next pointers
Not updating head after insert
4. Identify the error in this Go code that tries to insert a new node with value 7 at the beginning of a linked list:
type Node struct {
data int
next *Node
}
func insertAtBeginning(head *Node, val int) *Node {
newNode := &Node{data: val}
newNode.next = head.next
head = newNode
return head
}
medium
A. The function should not return head
B. head should not be reassigned inside the function
C. newNode.next should point to head, not head.next
D. newNode is not initialized correctly
Solution
Step 1: Check newNode.next assignment
It incorrectly points to head.next instead of head, skipping the current head node.
Step 2: Correct pointer to head
newNode.next must point to head to insert before it.
Final Answer:
newNode.next should point to head, not head.next -> Option C
Quick Check:
Link new node to head, not head.next [OK]
Hint: New node's next = head, not head.next [OK]
Common Mistakes:
Linking new node to head.next skipping head
Not updating head pointer correctly
Ignoring return value of updated head
5. Given a singly linked list with nodes containing integer values, write a Go function to insert multiple values at the beginning in order, so the final list reflects the insertion order. Which approach correctly achieves this?
hard
A. Insert each new value at the end one by one.
B. Insert each new value at the beginning but reverse the input slice before inserting.
C. Insert each new value at the beginning one by one.
D. Insert each new value at the end but reverse the input slice before inserting.
Solution
Step 1: Understand insertion order at beginning
Inserting at beginning reverses order because new nodes become head.
Step 2: Reverse input slice before inserting
Reversing input before inserting at beginning restores original order in final list.
Final Answer:
Insert each new value at the beginning but reverse the input slice before inserting. -> Option B
Quick Check:
Reverse input + head insert = original order [OK]
Hint: Reverse input before head inserts to keep order [OK]
Common Mistakes:
Inserting at beginning without reversing input
Inserting at end without reversing input
Confusing insert at beginning with insert at end
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.