Insert at Beginning Head Insert in DSA Typescript - 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.
class Node {
value: number;
next: Node | null = null;
constructor(value: number) {
this.value = value;
}
}
class LinkedList {
head: Node | null = null;
insertAtBeginning(value: number): void {
const newNode = new Node(value);
newNode.next = this.head;
this.head = newNode;
}
}
This code adds a new node at the start of a linked list by adjusting pointers.
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 recursion involved.
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 (same as above) |
Pattern observation: The number of steps stays the same regardless of list size.
Time Complexity: O(1)
This means adding at the start takes a fixed amount of time no matter how big the list is.
[X] Wrong: "Adding at the beginning takes longer as the list grows because we have to move all nodes."
[OK] Correct: We only change a couple of pointers; we do not move or visit other nodes.
Knowing that inserting at the head is quick helps you choose the right data structure for fast additions.
"What if we changed to insert at the end of the list? 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 head, pushing the old head to second position.Final Answer:
Adds a new node at the start of the list -> Option AQuick Check:
Insert at beginning = Add new head [OK]
- Confusing insert at beginning with insert at end
- Thinking it deletes nodes
- Assuming it changes the tail node
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 DQuick Check:
Link new node to head, then update head [OK]
- Assigning head before linking newNode.next
- Swapping assignments order
- Using head.next instead of newNode.next
class Node {
value: number;
next: Node | null = null;
constructor(value: number) { this.value = value; }
}
let head: Node | null = new Node(10);
let newNode = new Node(5);
newNode.next = head;
head = newNode;Solution
Step 1: Initial list setup
head points to node with value 10, next is null.Step 2: Insert newNode at beginning
newNode (5) points to head (10), then head updated to newNode.Final Answer:
5 -> 10 -> null -> Option CQuick Check:
New node 5 becomes head, points to 10 [OK]
- Confusing order of nodes after insertion
- Forgetting to update head
- Assuming newNode.next is null
class Node {
value: number;
next: Node | null = null;
constructor(value: number) { this.value = value; }
}
let head: Node | null = null;
let newNode = new Node(7);
head.next = newNode;
head = newNode;Solution
Step 1: Check initial head value
head is null, so accessing head.next is invalid and causes runtime error.Step 2: Correct insertion approach
Assign newNode.next = head (which is null), then head = newNode.Final Answer:
head is null, so head.next causes an error -> Option AQuick Check:
Cannot access property on null head [OK]
- Trying to access next on null head
- Not linking newNode.next to head
- Assigning head after using head.next
Solution
Step 1: Understand initial list
List is 20 -> 30 -> 40 -> null, head points to 20.Step 2: Insert 10 at beginning
Create new node 10, set newNode.next = head (20), then head = newNode.Final Answer:
10 -> 20 -> 30 -> 40 -> null -> Option BQuick Check:
New node 10 becomes head, old list follows [OK]
- Appending new node at end instead of beginning
- Mixing node order after insertion
- Not updating head pointer
Ready to prove it?
Pass the test and this pattern moves unknown
