What if you could remove the first item in a list with just one simple step?
Why Delete Node at Beginning in DSA Typescript?
Imagine you have a line of people waiting to buy tickets. If the first person leaves, you need to quickly update the line so the next person is now first.
If you try to remove the first person by checking each person one by one and shifting everyone forward manually, it takes a lot of time and can cause mistakes like losing track of who is next.
Using a linked list, you can remove the first person simply by changing the start point to the next person, making the update fast and error-free.
for(let i = 0; i < arr.length - 1; i++) { arr[i] = arr[i + 1]; } arr.pop();
head = head.next;
This lets you quickly and safely remove the first item from a list, making your program faster and easier to manage.
In a music playlist app, when a song finishes, deleting it from the start of the queue lets the next song play immediately without delays.
Manual shifting is slow and error-prone.
Deleting the first node by moving the head pointer is quick and simple.
This improves performance and code clarity.
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
