Push Using Linked List Node in DSA Typescript - 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.
class Node {
value: number;
next: Node | null = null;
constructor(value: number) {
this.value = value;
}
}
function push(head: Node | null, value: number): Node {
const newNode = new Node(value);
newNode.next = head;
return newNode;
}
This code adds a new node at the front of a linked list by creating it and linking it to the current head.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating a new node and updating pointers.
- How many times: This happens once per push call, no loops or recursion involved.
Adding a node always takes the same steps regardless of list size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 (create node, set next, return) |
| 100 | 3 (same steps, no extra work) |
| 1000 | 3 (still the same fixed steps) |
Pattern observation: The time stays the same no matter how big the list is.
Time Complexity: O(1)
This means adding a node at the front takes a constant amount of time, no matter the list size.
[X] Wrong: "Adding a node takes longer as the list grows because we have to move through the list."
[OK] Correct: We only change the head pointer and the new node's next pointer; we never traverse the list, so time does not grow with list size.
Knowing that pushing to the front of a linked list is quick and constant time 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 nodes?Solution
Step 1: Understand push operation in linked list
The push operation adds a new node to the start (head) of the linked list.Step 2: Effect of push on linked list
The new node becomes the new head, pointing to the previous first node.Final Answer:
Adds a new node at the beginning of the list -> Option AQuick Check:
Push means add at head = D [OK]
- Confusing push with pop (removal)
- Thinking push adds at the end
- Mixing push with search or delete
Solution
Step 1: Create a new node with given value
Usenew Node(value)to create a node instance.Step 2: Link new node to current head and update head
SetnewNode.next = headand thenhead = newNodeto push at front.Final Answer:
const newNode = new Node(value); newNode.next = head; head = newNode; -> Option CQuick Check:
New node points to old head, head updated = A [OK]
- Forgetting to link new node's next to old head
- Assigning head.next instead of newNode.next
- Not updating head to new node
class Node {
value: number;
next: Node | null = null;
constructor(value: number) { this.value = value; }
}
let head: Node | null = null;
function push(value: number) {
const newNode = new Node(value);
newNode.next = head;
head = newNode;
}
push(10);
push(20);
push(30);Solution
Step 1: Push 10 to empty list
Head becomes node with value 10, next is null.Step 2: Push 20 and then 30
Each new node points to previous head, so order is 30 -> 20 -> 10 -> null.Final Answer:
30 -> 20 -> 10 -> null -> Option DQuick Check:
Last pushed is head = A [OK]
- Assuming push adds at tail
- Reading list in wrong order
- Confusing next pointers
function push(value: number) {
const newNode = new Node(value);
head.next = newNode;
head = newNode;
}Solution
Step 1: Check head usage before assignment
The code useshead.nextwithout checking if head is null.Step 2: Consequence of head being null
If list is empty (head null), accessinghead.nextcauses runtime error.Final Answer:
head might be null causing runtime error -> Option AQuick Check:
Accessing head.next without null check = B [OK]
- Not checking for empty list before push
- Forgetting to set newNode.next
- Assigning head incorrectly
Also, explain how the head changes after each push.
Solution
Step 1: Initial list and push 35
Original head is 5. Push 35 creates new node 35 pointing to 5. Head updates to 35.Step 2: Push 45 and update head
Push 45 creates new node 45 pointing to 35. Head updates to 45. Final list: 45 -> 35 -> 5 -> 15 -> 25 -> null.Final Answer:
45 -> 35 -> 5 -> 15 -> 25 -> null; head moves to newest node each push -> Option BQuick Check:
Push adds at front, head updates = C [OK]
- Assuming push adds at tail
- Not updating head after push
- Confusing node order after pushes
Ready to prove it?
Pass the test and this pattern moves unknown
