Insert at Beginning Head Insert in DSA Go - Time & Space Complexity
We want to understand how long it takes to add a new item 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.
// Insert a new node at the beginning of the list
func InsertAtBeginning(head *Node, val int) *Node {
newNode := &Node{value: val}
newNode.next = head
head = newNode
return head
}
This code creates a new node and places it at the start of the linked list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating a new node and updating pointers.
- How many times: This happens once per insertion, no loops or traversals.
Adding a node at the start always takes the same steps, no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 steps (create node, set next, update head) |
| 100 | 3 steps (same as above) |
| 1000 | 3 steps (still the same) |
Pattern observation: The number of steps stays constant regardless of list size.
Time Complexity: O(1)
This means adding at the beginning takes the same short time no matter how big the list is.
[X] Wrong: "Adding at the start takes longer as the list grows because we have to move all nodes."
[OK] Correct: We only change a few pointers; we do not move or visit other nodes.
Knowing that inserting at the head is quick helps you choose the right method for fast updates in linked lists.
"What if we changed to insert at the end of the list instead? How would the time complexity change?"
Practice
Solution
Step 1: Understand the head pointer role
The head pointer points to the first node in the list.Step 2: Insert at beginning changes head
Inserting at beginning means creating a new node and making it the new head, linking it to the old head.Final Answer:
Adds a new node at the start of the list, making it the new head -> Option DQuick Check:
Insert at beginning = new head node [OK]
- Confusing insert at beginning with insert at end
- Thinking it deletes nodes
- Assuming it searches nodes
Solution
Step 1: Create new node pointing to current head
newNode is created with data 10 and next pointing to current head.Step 2: Update head to new node
Assign head to newNode so it becomes the first node.Final Answer:
newNode := &Node{data: 10, next: head}\nhead = newNode -> Option AQuick Check:
New node points to old head, head updated [OK]
- Not linking new node to old head
- Changing head.next instead of head
- Forgetting to update head pointer
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")
}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 AQuick Check:
Head insert order reversed list [OK]
- Printing in insertion order instead of head order
- Confusing next pointers
- Not updating head after insert
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
}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 CQuick Check:
Link new node to head, not head.next [OK]
- Linking new node to head.next skipping head
- Not updating head pointer correctly
- Ignoring return value of updated head
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 BQuick Check:
Reverse input + head insert = original order [OK]
- Inserting at beginning without reversing input
- Inserting at end without reversing input
- Confusing insert at beginning with insert at end
Ready to prove it?
Pass the test and this pattern moves unknown
