What if you could remove the first item instantly without moving everyone else?
Why Delete Node at Beginning in DSA Javascript?
Imagine you have a line of people waiting to buy tickets. If the first person leaves, everyone else has to move forward manually to fill the gap.
Manually shifting everyone forward is slow and confusing. You might lose track of who is next or accidentally skip someone.
Using a linked list, you can simply remove the first person by changing the start point to the next person, without moving anyone else.
let queue = ["Alice", "Bob", "Charlie"]; queue.shift(); // removes first but shifts all elements
let head = {value: "Alice", next: {value: "Bob", next: {value: "Charlie", next: null}}};
head = head.next; // removes first node by moving headYou can quickly remove the first item without touching the rest, making operations fast and simple.
In a printer queue, when a print job finishes, it is removed instantly from the front so the next job starts immediately.
Manual shifting is slow and error-prone.
Linked lists let you remove the first node by just moving the head pointer.
This makes deleting at the beginning fast and easy.
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
