Delete Node at Beginning in DSA Typescript - Time & Space Complexity
We want to understand how long it takes to remove the first node from 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;
constructor(value: number) {
this.value = value;
this.next = null;
}
}
function deleteAtBeginning(head: Node | null): Node | null {
if (head === null) return null;
return head.next;
}
This code removes the first node from a linked list by returning the second node as the new head.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing the first node and its next pointer.
- How many times: Exactly once, no loops or recursion.
Removing the first node always takes the same steps, no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The work stays the same even if the list grows larger.
Time Complexity: O(1)
This means deleting the first node takes a fixed amount of time regardless of list size.
[X] Wrong: "Deleting the first node takes longer if the list is bigger because there are more nodes."
[OK] Correct: We only change the head pointer to the next node, so the rest of the list size does not affect the time.
Knowing that deleting the first node is quick helps you explain efficient list operations clearly and confidently.
"What if we had to delete the last node 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: Delete the first node by moving head
Deleting the first node means moving the head pointer to the next node.Final Answer:
The head pointer moves to the second node -> Option AQuick Check:
Delete first node = move head to next [OK]
- Thinking the last node is deleted
- Assuming list becomes empty always
- Deleting a middle node instead
head?Solution
Step 1: Understand head and next pointers
head points to first node; head.next points to second node.Step 2: Assign head to head.next to delete first node
Setting head = head.next moves head to second node, effectively deleting first.Final Answer:
head = head.next; -> Option AQuick Check:
Correct syntax to delete first node = head = head.next; [OK]
- Using delete keyword incorrectly
- Assigning head.next to head
- Accessing null.next causing error
1 -> 2 -> 3 -> null and the code snippet:head = head.next;
What is the resulting linked list?
Solution
Step 1: Initial list and head pointer
Head points to node with value 1, list is 1 -> 2 -> 3 -> null.Step 2: Move head to head.next
head = head.next moves head to node with value 2, removing first node.Final Answer:
2 -> 3 -> null -> Option BQuick Check:
After deletion, list starts at 2 [OK]
- Assuming list remains unchanged
- Thinking last node is deleted
- Confusing node values after deletion
if (head !== null) {
head = head.next;
}What error does this fix compared to just
head = head.next;?Solution
Step 1: Understand head could be null
If list is empty, head is null and head.next causes error.Step 2: Check head before accessing next
Condition ensures head is not null before moving to head.next, avoiding runtime error.Final Answer:
Prevents runtime error if list is empty -> Option CQuick Check:
Check head != null before head.next avoids error [OK]
- Ignoring null head causing crash
- Thinking it prevents memory leaks
- Confusing node positions deleted
10 -> 20 -> 30 -> 40 -> null. You want to delete the first two nodes safely. Which code snippet correctly does this?Solution
Step 1: Understand deleting two nodes means moving head twice
Deleting first two nodes means head should point to third node.Step 2: Check head and head.next exist before moving head twice
Condition ensures no runtime error when accessing head.next.next.Final Answer:
if (head && head.next) { head = head.next.next; } -> Option DQuick Check:
Check head and head.next before double move [OK]
- Not checking head.next causing error
- Assigning head.next instead of head
- Setting head to null deletes entire list
Ready to prove it?
Pass the test and this pattern moves unknown
