Bird
Raised Fist0
DSA Javascriptprogramming~10 mins

Delete Node at Beginning in DSA Javascript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Delete Node at Beginning
Start
↓
Check if list is empty?
Yes→Return null or empty
No↓
Set head to head.next
↓
Old head node removed
↓
Return new head
↓
End
The flow checks if the list is empty, then moves the head pointer to the next node, effectively removing the first node.
Execution Sample
DSA Javascript
function deleteAtBeginning(head) {
  if (!head) return null;
  return head.next;
}
This function removes the first node of a linked list by returning the second node as the new head.
Execution Table
Stephead (before)Condition (!head)Actionhead (after)Output
1{1 -> 2 -> 3 -> null}falseSet head = head.next{2 -> 3 -> null}{2 -> 3 -> null}
2{2 -> 3 -> null}falseSet head = head.next{3 -> null}{3 -> null}
3{3 -> null}falseSet head = head.nextnullnull
4nulltrueReturn nullnullnull
💡 Execution stops when head is null, meaning the list is empty.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
head{1 -> 2 -> 3 -> null}{2 -> 3 -> null}{3 -> null}nullnull
Key Moments - 3 Insights
Why do we check if head is null before deleting?
Because if head is null, the list is empty and there is no node to delete. This is shown in execution_table step 4 where condition is true and function returns null.
What happens to the old first node after deletion?
The old first node is no longer referenced by head and will be removed by garbage collection. The list now starts from the second node, as seen in execution_table step 1 where head changes from {1 -> 2 -> 3} to {2 -> 3}.
Can we delete from a list with only one node?
Yes, deleting the first node in a single-node list results in an empty list (head becomes null), as shown in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of head after step 2?
A{3 -> null}
B{2 -> 3 -> null}
Cnull
D{1 -> 2 -> 3 -> null}
💡 Hint
Check the 'head (after)' column in execution_table row for step 2.
At which step does the list become empty?
AStep 1
BStep 3
CStep 4
DStep 2
💡 Hint
Look for when head becomes null in the variable_tracker and execution_table.
If the initial list is empty (head is null), what does the function return?
AAn error
BA new node
CThe same null value
DThe original head
💡 Hint
See execution_table step 4 where condition is true and function returns null.
Concept Snapshot
Delete Node at Beginning:
- Check if list is empty (head == null)
- If empty, return null
- Else, move head to head.next
- Old first node is removed
- New head is returned
Full Transcript
This visual execution shows how to delete the first node of a linked list. We start by checking if the list is empty. If it is, we return null immediately. Otherwise, we move the head pointer to the next node, effectively removing the first node. The old first node is no longer referenced and will be cleaned up. The process repeats until the list becomes empty. The execution table tracks the head pointer and actions at each step, while the variable tracker shows how head changes over time. Key moments clarify why we check for empty lists and what happens to nodes after deletion.

Practice

(1/5)
1. What happens to the head pointer when you delete the first node in a singly linked list?
easy
A. It moves to the next node in the list
B. It becomes null immediately
C. It points to the last node
D. It remains unchanged

Solution

  1. Step 1: Understand the head pointer role

    The head pointer points to the first node in the list.
  2. 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.
  3. Final Answer:

    It moves to the next node in the list -> Option A
  4. Quick Check:

    Head points to next node after deletion [OK]
Hint: Head always moves to next node when deleting first [OK]
Common Mistakes:
  • Assuming head becomes null even if list has more nodes
  • Thinking head points to last node after deletion
  • Not updating head pointer at all
2. Which of the following is the correct syntax to delete the first node in a singly linked list in JavaScript?
easy
A. delete head;
B. head.next = head;
C. head = head.next;
D. head = null.next;

Solution

  1. Step 1: Understand how to move head pointer

    To delete the first node, assign head to head.next to skip the first node.
  2. 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.
  3. Final Answer:

    head = head.next; -> Option C
  4. Quick Check:

    Assign head to head.next to delete first node [OK]
Hint: Use head = head.next to remove first node [OK]
Common Mistakes:
  • Using delete keyword incorrectly
  • Assigning head.next to head instead of head to head.next
  • Using invalid syntax like null.next
3. Consider the following linked list code in JavaScript:
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?
medium
A. []
B. [10, 20, 30]
C. [30]
D. [20, 30]

Solution

  1. Step 1: Initial linked list nodes

    The list has nodes with data 10 -> 20 -> 30.
  2. Step 2: Deleting first node by head = head.next

    Head moves from node 10 to node 20, so node 10 is removed.
  3. Step 3: Printing remaining nodes

    Starting from new head (20), the list is 20 -> 30, so result array is [20, 30].
  4. Final Answer:

    [20, 30] -> Option D
  5. Quick Check:

    After deletion, list starts at 20 [OK]
Hint: Deleting first node skips first data in output [OK]
Common Mistakes:
  • Printing original list without deletion
  • Confusing head.next with head
  • Forgetting to update head pointer
4. The following code attempts to delete the first node of a linked list but causes an error:
head.next = head.next.next;

What is the problem with this code if head points to the first node?
medium
A. It deletes the second node instead of the first node
B. It causes a runtime error if head.next is null
C. It correctly deletes the first node
D. It sets head to null incorrectly

Solution

  1. 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.
  2. Step 2: Check if first node is deleted

    The head pointer still points to the first node, so the first node is not deleted.
  3. Final Answer:

    It deletes the second node instead of the first node -> Option A
  4. Quick Check:

    head.next changes second node, not head itself [OK]
Hint: Changing head.next skips second node, not first [OK]
Common Mistakes:
  • Thinking this deletes the first node
  • Ignoring possible null pointer if list has one node
  • Confusing head and head.next pointers
5. You have a singly linked list with nodes: 5 -> 10 -> 15 -> 20. Write a function in JavaScript to delete the first node and return the new head. What will be the list after deletion?
hard
A. 20
B. 10 -> 15 -> 20
C. 15 -> 20
D. 5 -> 10 -> 15 -> 20

Solution

  1. Step 1: Write function to delete first node

    Function returns head.next to skip first node:
    function deleteFirstNode(head) { return head ? head.next : null; }
  2. Step 2: Apply function to list 5 -> 10 -> 15 -> 20

    New head points to node with data 10, so list becomes 10 -> 15 -> 20.
  3. Final Answer:

    10 -> 15 -> 20 -> Option B
  4. Quick Check:

    Deleting first node moves head to second node [OK]
Hint: Return head.next to delete first node [OK]
Common Mistakes:
  • Returning head instead of head.next
  • Not handling empty list (null head)
  • Deleting wrong node by changing next pointers incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard