Delete Node at Beginning in DSA Java - Time & Space Complexity
We want to know how long it takes to remove the first item from a list.
How does the time needed change when the list grows bigger?
Analyze the time complexity of the following code snippet.
class Node {
int data;
Node next;
}
Node deleteAtBeginning(Node head) {
if (head == null) return null;
return head.next;
}
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 the head pointer.
- How many times: This happens only once, no loops or repeated steps.
Removing the first node takes the same amount of time no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time 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 we have to check all nodes."
[OK] Correct: We only change the head pointer; no need to look at other nodes.
Knowing this helps you explain how simple operations on linked lists work fast, a key skill in many coding tasks.
"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 always points to the first node in the list.Step 2: Effect of deleting the first node
Deleting the first node means the head must 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]
- Forgetting to update head pointer
- Setting head to null without checking list
- Pointing head to last node incorrectly
Solution
Step 1: Understand the head pointer update
To delete the first node, head must point to the second node, which is head.next.Step 2: Check each option
head = head.next; correctly assigns head to head.next. Others either cause errors or wrong assignments.Final Answer:
head = head.next; -> Option AQuick Check:
Assign head to next node to delete first [OK]
- Using head.next = head which reverses links
- Trying to access null.next causing error
- Setting head.next to null which breaks list
head = head.next;?Solution
Step 1: Identify the initial list
The list starts as 10 -> 20 -> 30 -> null, with head pointing to 10.Step 2: Apply deletion by moving head
Setting head = head.next moves head to 20, so the list becomes 20 -> 30 -> null.Final Answer:
20 -> 30 -> null -> Option BQuick Check:
Head moves from 10 to 20 after deletion [OK]
- Not updating head pointer
- Assuming first node remains
- Confusing node values after deletion
if (head == null) {
System.out.println("List is empty");
} else {
head.next = head;
}Solution
Step 1: Analyze the assignment head.next = head
This sets the next pointer of the first node to point back to itself, creating a cycle.Step 2: Understand the impact on deletion
The head pointer is not updated, so the first node is not removed and the list structure is broken.Final Answer:
It causes the head to point to itself, creating a cycle -> Option DQuick Check:
Incorrect pointer assignment creates cycle [OK]
- Confusing head.next = head with head = head.next
- Not checking for empty list before deletion
- Ignoring pointer updates causing cycles
head = head.next;, what will be the final list?Solution
Step 1: Initial list and first deletion
Start: 5 -> 15 -> 25 -> 35 -> null. After first deletion, head moves to 15, list is 15 -> 25 -> 35 -> null.Step 2: Second deletion
After second deletion, head moves to 25, list is 25 -> 35 -> null.Final Answer:
25 -> 35 -> null -> Option CQuick Check:
Two deletions remove first two nodes [OK]
- Deleting only once instead of twice
- Confusing node values after multiple deletions
- Not updating head pointer correctly each time
Ready to prove it?
Pass the test and this pattern moves unknown
