Delete Node at Beginning in DSA Javascript - 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 when the list grows bigger?
Analyze the time complexity of the following code snippet.
function deleteAtBeginning(head) {
if (head === null) return null;
let newHead = head.next;
head.next = null;
return newHead;
}
This code removes the first node from a linked list and returns the new head.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing the first node and changing pointers.
- How many times: This happens only 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 | 3 steps |
| 100 | 3 steps |
| 1000 | 3 steps |
Pattern observation: The number of steps stays the same even if the list grows.
Time Complexity: O(1)
This means deleting the first node takes the same amount of time no matter how big the list is.
[X] Wrong: "Deleting the first node takes longer if the list is bigger because we have to check all nodes."
[OK] Correct: We only change the first node's pointer, so the list size does not affect the time.
Knowing this helps you explain why some operations are quick and others take longer, which is a key skill in coding interviews.
"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: Effect of deleting the first node
Deleting the first node means the head must now point to the second node, which becomes the new first node.Final Answer:
It moves to the next node in the list -> Option AQuick Check:
Head points to next node after deletion [OK]
- Assuming head becomes null even if list has more nodes
- Thinking head points to last node after deletion
- Not updating head pointer at all
Solution
Step 1: Understand how to move head pointer
To delete the first node, assign head to head.next to skip the first node.Step 2: Check each option
head = head.next; correctly assigns head to head.next. head.next = head; reverses pointers incorrectly. delete head; deletes the variable but not the node. head = null.next; is invalid syntax.Final Answer:
head = head.next; -> Option CQuick Check:
Assign head to head.next to delete first node [OK]
- Using delete keyword incorrectly
- Assigning head.next to head instead of head to head.next
- Using invalid syntax like null.next
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
let head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
// Delete first node
head = head.next;
// Print list
let current = head;
let result = [];
while(current) {
result.push(current.data);
current = current.next;
}
console.log(result);What will be printed?
Solution
Step 1: Initial linked list nodes
The list has nodes with data 10 -> 20 -> 30.Step 2: Deleting first node by head = head.next
Head moves from node 10 to node 20, so node 10 is removed.Step 3: Printing remaining nodes
Starting from new head (20), the list is 20 -> 30, so result array is [20, 30].Final Answer:
[20, 30] -> Option DQuick Check:
After deletion, list starts at 20 [OK]
- Printing original list without deletion
- Confusing head.next with head
- Forgetting to update head pointer
head.next = head.next.next;
What is the problem with this code if head points to the first node?
Solution
Step 1: Analyze what head.next = head.next.next does
This changes the next pointer of the first node to skip the second node, effectively deleting the second node.Step 2: Check if first node is deleted
The head pointer still points to the first node, so the first node is not deleted.Final Answer:
It deletes the second node instead of the first node -> Option AQuick Check:
head.next changes second node, not head itself [OK]
- Thinking this deletes the first node
- Ignoring possible null pointer if list has one node
- Confusing head and head.next pointers
Solution
Step 1: Write function to delete first node
Function returns head.next to skip first node:
function deleteFirstNode(head) { return head ? head.next : null; }Step 2: Apply function to list 5 -> 10 -> 15 -> 20
New head points to node with data 10, so list becomes 10 -> 15 -> 20.Final Answer:
10 -> 15 -> 20 -> Option BQuick Check:
Deleting first node moves head to second node [OK]
- Returning head instead of head.next
- Not handling empty list (null head)
- Deleting wrong node by changing next pointers incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
