What if you could remove the first item from a list with just one simple step?
Why Delete Node at Beginning in DSA Java?
Imagine you have a long paper chain where each paper is linked to the next. To remove the first paper, you have to carefully unhook it without breaking the chain.
Doing this by hand is slow and risky. You might accidentally tear the chain or lose track of the next paper, making the whole chain useless.
Using a linked list and deleting the first node means you just move the start pointer to the next node. This is quick and safe, like sliding off the first paper without breaking the chain.
Node temp = head;
while(temp.next != null) {
// shifting nodes manually
temp = temp.next;
}
head = head.next;head = head.next;
This lets you quickly remove the first item from a list without touching the rest, making data updates fast and error-free.
Think of a queue at a ticket counter where the first person leaves after buying a ticket. Removing them quickly lets the next person step up without confusion.
Manual removal of the first item is slow and error-prone.
Deleting the first node in a linked list is just moving the head pointer.
This makes list updates fast, safe, and simple.
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
