Insert at Beginning Head Insert in DSA Javascript - 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 bigger?
Analyze the time complexity of the following code snippet.
function insertAtBeginning(head, value) {
const newNode = { value: value, next: head };
return newNode;
}
This code creates a new node and places it at the start of the list, making it the new head.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating one new node and linking it to the current head.
- 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 | 1 insertion step |
| 100 | 1 insertion step |
| 1000 | 1 insertion step |
Pattern observation: The time stays constant as the list grows.
Time Complexity: O(1)
This means adding at the start takes the same short time no matter how many items are in the list.
[X] Wrong: "Adding at the start takes longer as the list grows because the list is bigger."
[OK] Correct: We only change the first node's pointer, no need to look through the list, so time stays the same.
Knowing that head insertion is quick helps you choose the right method when speed matters 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 operation
Inserting at the beginning means adding a new node before the current first node.Step 2: Effect on the list
The new node becomes the new head, pushing the old head to second position.Final Answer:
Adds a new node at the start of the list -> Option CQuick Check:
Insert at beginning = Add new head [OK]
- Confusing insert at beginning with insert at end
- Thinking it deletes nodes
- Assuming it modifies middle nodes
Solution
Step 1: Link new node to current head
Set newNode.next to point to current head node.Step 2: Update head to new node
Assign head to newNode so it becomes the first node.Final Answer:
newNode.next = head; head = newNode; -> Option AQuick Check:
Link new node to head, then update head [OK]
- Swapping assignments incorrectly
- Not updating head pointer
- Assigning head.next instead of newNode.next
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
let head = new Node(10);
let newNode = new Node(20);
newNode.next = head;
head = newNode;
// Print list
let current = head;
let result = [];
while(current) {
result.push(current.data);
current = current.next;
}
console.log(result);Solution
Step 1: Insert newNode at beginning
newNode (20) points to head (10), then head updated to newNode.Step 2: Traverse list from new head
Traversal order: 20 -> 10 -> null, so result array is [20, 10].Final Answer:
[20, 10] -> Option AQuick Check:
New node at head means list starts with 20 [OK]
- Reversing order of nodes in output
- Ignoring head update
- Confusing data values
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
let head = new Node(5);
let newNode = new Node(15);
head = newNode;
newNode.next = head;Solution
Step 1: Analyze assignment order
head is set to newNode first, then newNode.next is set to head (which is newNode itself).Step 2: Detect cycle creation
This causes newNode.next to point to itself, creating a cycle and breaking the list.Final Answer:
The newNode.next is assigned after head is updated, causing a cycle -> Option DQuick Check:
Assign next before updating head to avoid cycle [OK]
- Assigning head before newNode.next
- Ignoring pointer references
- Assuming newNode.next should be null always
Solution
Step 1: Insert 40 at beginning
New head becomes 40, list is [40, 30, 20, 10].Step 2: Insert 50 at beginning
New head becomes 50, list is [50, 40, 30, 20, 10].Final Answer:
[50, 40, 30, 20, 10] -> Option BQuick Check:
Each insert adds new head, order reverses insert sequence [OK]
- Appending instead of prepending
- Reversing insertion order
- Not updating head after each insert
Ready to prove it?
Pass the test and this pattern moves unknown
