What does the 'push' operation do in a linked list?
It adds a new node at the beginning (head) of the linked list, making it the new first element.
Click to reveal answer
beginner
In Go, what is the key step to link a new node to the existing linked list during a push?
Set the new node's 'next' pointer to the current head node, then update the head to the new node.
Click to reveal answer
beginner
Why do we update the head pointer after pushing a new node?
Because the new node becomes the first node, so the head must point to it to keep the list accessible.
Click to reveal answer
beginner
What is the time complexity of the push operation in a linked list?
O(1) — It takes constant time because it only changes a few pointers without traversing the list.
Click to reveal answer
beginner
Show the basic Go code snippet to push a node with value 10 to a linked list.
newNode := &Node{data: 10}
newNode.next = head
head = newNode
Click to reveal answer
What pointer does the new node's 'next' field point to after a push?
AThe last node
BNil
CThe current head node
DItself
✗ Incorrect
The new node's 'next' points to the current head to link the new node before the existing list.
After pushing a new node, what does the head pointer point to?
AThe old head node
BThe new node
CNil
DThe last node
✗ Incorrect
The head pointer updates to the new node because it is now the first node in the list.
What is the time complexity of pushing a node to the front of a linked list?
AO(1)
BO(n)
CO(log n)
DO(n^2)
✗ Incorrect
Push operation takes constant time because it only changes pointers without traversing the list.
Which of these is NOT a step in pushing a node to a linked list?
ACreate a new node
BSet new node's next to current head
CUpdate head to new node
DTraverse to the last node
✗ Incorrect
Push adds at the front, so no traversal to the last node is needed.
If the linked list is empty (head is nil), what happens when you push a new node?
ANew node becomes the head with next as nil
BPush fails
CNew node's next points to itself
DHead remains nil
✗ Incorrect
When the list is empty, the new node becomes the head and its next pointer is nil.
Explain step-by-step how to push a new node onto a linked list in Go.
Think about how the new node connects to the existing list and how the head changes.
You got /3 concepts.
Describe why the push operation on a linked list is efficient.
Consider what makes push faster than adding at the end.
You got /3 concepts.
Practice
(1/5)
1. What does the push operation do in a linked list when using a node?
easy
A. Deletes the entire linked list
B. Removes the last node from the list
C. Searches for a node with a specific value
D. Adds a new node at the beginning of the list
Solution
Step 1: Understand push operation in linked list
The push operation adds a new node at the start (head) of the linked list.
Step 2: Effect of push on list structure
The new node becomes the head, and its next pointer points to the previous head node.
Final Answer:
Adds a new node at the beginning of the list -> Option D
Quick Check:
Push = Add at head [OK]
Hint: Push always adds node at the start of the list [OK]
Common Mistakes:
Thinking push adds at the end
Confusing push with pop
Assuming push deletes nodes
2. Which of the following is the correct syntax to create a new node and push it onto a linked list in Go?
easy
A. newNode := Node{data: 5}; head.next = newNode
B. newNode := &Node{data: 5, next: head}; head = newNode
C. newNode := &Node{5}; head = newNode.next
D. newNode := Node{data: 5, next: nil}; head = newNode.next
Solution
Step 1: Create a new node with data and next pointer
In Go, use &Node{data: 5, next: head} to create a new node pointing to current head.
Step 2: Update head to new node
Assign head = newNode to make newNode the new head of the list.
Final Answer:
newNode := &Node{data: 5, next: head}; head = newNode -> Option B
Quick Check:
New node points to head, then head updated [OK]
Hint: New node's next points to head, then head updated [OK]
Common Mistakes:
Not using pointer (&) for new node
Assigning head.next instead of head
Setting next to nil instead of head
3. What will be the output linked list after executing this Go code?
type Node struct {
data int
next *Node
}
func main() {
var head *Node = nil
head = &Node{data: 1, next: head}
head = &Node{data: 2, next: head}
head = &Node{data: 3, next: head}
printList(head)
}
func printList(node *Node) {
for node != nil {
fmt.Print(node.data, " -> ")
node = node.next
}
fmt.Print("null")
}
medium
A. 3 -> 2 -> 1 -> null
B. 1 -> 2 -> 3 -> null
C. null
D. 1 -> null
Solution
Step 1: Trace node insertions
First node with data 1 is pushed, head points to node 1. Then node 2 is pushed, head points to node 2, next points to node 1. Then node 3 is pushed, head points to node 3, next points to node 2.
Step 2: Print list from head
Prints 3 -> 2 -> 1 -> null as nodes are added at front.
Final Answer:
3 -> 2 -> 1 -> null -> Option A
Quick Check:
Push adds at head, so list reversed [OK]
Hint: Push adds at head, so last pushed is first in list [OK]
Common Mistakes:
Assuming push adds at tail
Confusing order of nodes
Ignoring nil termination
4. Identify the error in this Go code that tries to push a new node onto a linked list:
type Node struct {
data int
next *Node
}
func push(head *Node, data int) {
newNode := &Node{data: data, next: head}
head = newNode
}
func main() {
var head *Node = nil
push(head, 10)
// printList(head) expected to show 10 -> null
}
medium
A. head is passed by value, so main's head is not updated
B. newNode is not created correctly with & operator
C. next pointer should be nil, not head
D. Function push should return void, not update head
Solution
Step 1: Analyze parameter passing
head is passed by value to push, so changes inside push do not affect main's head.
Step 2: Effect on linked list head
Assigning head = newNode inside push only changes local copy, main's head remains nil.
Final Answer:
head is passed by value, so main's head is not updated -> Option A
Quick Check:
Pointer must be passed by reference or returned [OK]
Hint: Pass head pointer by reference or return new head [OK]
Common Mistakes:
Assuming pointer changes reflect outside function
Not returning updated head
Misunderstanding Go parameter passing
5. Given a linked list with nodes containing integers, how would you modify the push function to avoid memory leaks and ensure the list is correctly updated in Go?
hard
A. Use a slice instead of linked list for better memory management
B. Use a global variable for head instead of passing it
C. Make push return the new head node and assign it to head in main
D. Set newNode.next to nil before assigning head
Solution
Step 1: Understand pointer update requirement
In Go, to update head outside push, function must return new head node.
Step 2: Assign returned head in main
Call head = push(head, data) to update head correctly and avoid dangling pointers.
Final Answer:
Make push return the new head node and assign it to head in main -> Option C
Quick Check:
Return new head to update list safely [OK]
Hint: Return new head from push and assign it outside [OK]
Common Mistakes:
Not returning updated head
Using global variables unnecessarily
Ignoring pointer semantics in Go
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.