Push Using Linked List Node in DSA Go - Time & Space Complexity
We want to understand how long it takes to add a new node at the start of a linked list.
How does the time needed change as the list grows?
Analyze the time complexity of the following code snippet.
type Node struct {
data int
next *Node
}
func push(head **Node, newData int) {
newNode := &Node{data: newData}
newNode.next = *head
*head = newNode
}
This code adds a new node at the front of a linked list by adjusting pointers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating a new node and changing pointers.
- How many times: This happens once per push call, no loops or recursion inside.
Adding a node always takes the same steps regardless of list size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 steps |
| 100 | 3 steps |
| 1000 | 3 steps |
Pattern observation: The number of steps stays the same no matter how big the list is.
Time Complexity: O(1)
This means adding a node at the front takes a fixed amount of time, no matter the list size.
[X] Wrong: "Adding a node takes longer as the list grows because we have more nodes."
[OK] Correct: We only change the first node's pointer, so the rest of the list size does not affect the time.
Knowing that pushing to a linked list is quick helps you explain efficient data structure operations clearly.
"What if we had to add a node at the end of the linked list instead? How would the time complexity change?"
Practice
push operation do in a linked list when using a node?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 DQuick Check:
Push = Add at head [OK]
- Thinking push adds at the end
- Confusing push with pop
- Assuming push deletes nodes
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 BQuick Check:
New node points to head, then head updated [OK]
- Not using pointer (&) for new node
- Assigning head.next instead of head
- Setting next to nil instead of head
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")
}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 AQuick Check:
Push adds at head, so list reversed [OK]
- Assuming push adds at tail
- Confusing order of nodes
- Ignoring nil termination
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
}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 AQuick Check:
Pointer must be passed by reference or returned [OK]
- Assuming pointer changes reflect outside function
- Not returning updated head
- Misunderstanding Go parameter passing
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 CQuick Check:
Return new head to update list safely [OK]
- Not returning updated head
- Using global variables unnecessarily
- Ignoring pointer semantics in Go
Ready to prove it?
Pass the test and this pattern moves unknown
