Bird
Raised Fist0
DSA Javascriptprogramming~10 mins

Delete from Beginning of Doubly Linked List in DSA Javascript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Delete from Beginning of Doubly Linked List
Start at head
↓
Check if head is null?
Yes→List is empty, nothing to delete
No↓
Move head to head.next
↓
If new head exists, set new head.prev = null
↓
Old head node is removed
↓
Done
Start at the head node, check if list is empty. If not, move head to next node and update pointers to remove old head.
Execution Sample
DSA Javascript
function deleteFromBeginning() {
  if (!head) return;
  head = head.next;
  if (head) head.prev = null;
}
Deletes the first node of a doubly linked list by moving head to next and updating pointers.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Initial list10 <-> 20 <-> 30head → node 10ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:10│←── │ prev:20│ │ next:20│──→ │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
2Check if head is null10 <-> 20 <-> 30No changeSame as step 1
3Move head to head.next20 <-> 30head → node 20ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:10│←── │ prev:20│ │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
4Set new head.prev = null20 <-> 30node 20.prev = nullā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:20│ │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
5Old head (10) removed20 <-> 30node 10 disconnectedā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:20│ │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
6End20 <-> 30No changeSame as step 5
šŸ’” Head moved to next node; old head removed; list updated accordingly.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
headnode 10node 20node 20node 20
head.prevnullnode 10nullnull
old head (node 10)connecteddisconnecteddisconnecteddisconnected
list size3 nodes3 nodes3 nodes2 nodes (conceptual)
Key Moments - 3 Insights
Why do we set new head.prev to null after moving head?
Because the new head is now the first node, it should not point back to any previous node. See step 4 in execution_table where node 20.prev changes from node 10 to null.
What happens if the list is empty when we try to delete from beginning?
The function checks if head is null at step 2 and returns immediately, so no changes happen and no error occurs.
Why do we not need to explicitly delete the old head node in JavaScript?
Because after removing references (head moved and old node disconnected), JavaScript's garbage collector automatically frees memory. See step 5 where node 10 is disconnected.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what does the head pointer point to?
ANode with data 30
BNode with data 10
CNode with data 20
DNull
šŸ’” Hint
Check the 'Pointer Changes' column at step 3 in execution_table.
At which step does the new head's prev pointer become null?
AStep 4
BStep 3
CStep 2
DStep 5
šŸ’” Hint
Look at the 'Pointer Changes' and 'Visual State' columns in execution_table for step 4.
If the list was empty initially, what would happen at step 2?
AHead moves to next node
BFunction returns immediately
COld head is removed
DNew head.prev is set to null
šŸ’” Hint
Step 2 checks if head is null and returns if true, as shown in execution_table.
Concept Snapshot
Delete from Beginning of Doubly Linked List:
- Check if list is empty (head == null)
- Move head pointer to head.next
- If new head exists, set new head.prev = null
- Old head node is disconnected and removed
- List size decreases by one
- Garbage collector frees old node automatically
Full Transcript
To delete the first node in a doubly linked list, start by checking if the list is empty. If it is not, move the head pointer to the next node. Then update the new head's prev pointer to null because it is now the first node. The old head node is disconnected and will be removed by garbage collection. This process reduces the list size by one and keeps the list properly linked.

Practice

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

Solution

  1. Step 1: Understand the head pointer role

    The head pointer always points to the first node in the list.
  2. Step 2: Effect of deleting the first node

    When the first node is deleted, the head pointer must move to the next node, which becomes the new first node.
  3. Final Answer:

    It moves to the second node in the list -> Option B
  4. Quick Check:

    Head points to new first node after deletion [OK]
Hint: Head always points to the first node after deletion [OK]
Common Mistakes:
  • Assuming head becomes null without checking list length
  • Not updating head pointer after deletion
  • Pointing head to last node mistakenly
2. Which of the following is the correct syntax to delete the first node from a doubly linked list in JavaScript?
easy
A. head.prev = null; head = head.next;
B. head = null; head.next = null;
C. head.next = null; head = head.prev;
D. head = head.next; head.prev = null;

Solution

  1. Step 1: Move head to next node

    Assign head to head.next to remove the first node reference.
  2. Step 2: Update new head's prev pointer

    Set head.prev to null to disconnect backward link from old first node.
  3. Final Answer:

    head = head.next; head.prev = null; -> Option D
  4. Quick Check:

    Correct syntax updates head and prev pointer [OK]
Hint: Update head first, then set new head.prev to null [OK]
Common Mistakes:
  • Setting head.prev before moving head
  • Setting head.next to null incorrectly
  • Nullifying head before updating pointers
3. Consider the doubly linked list: 10 <-> 20 <-> 30 <-> null. After deleting the first node, what will be the new list?
medium
A. 30 <-> null
B. 10 <-> 30 <-> null
C. 20 <-> 30 <-> null
D. null

Solution

  1. Step 1: Identify the first node

    The first node is 10, which will be removed.
  2. Step 2: Update head to second node

    Head moves to 20, and 20.prev is set to null.
  3. Final Answer:

    20 <-> 30 <-> null -> Option C
  4. Quick Check:

    First node removed, list starts at 20 [OK]
Hint: After deletion, list starts from second node [OK]
Common Mistakes:
  • Leaving old head in list
  • Not updating prev pointer of new head
  • Removing wrong node
4. Given this code snippet to delete the first node, what is the error?
if (head !== null) {
  head = head.next;
  head.prev = null;
}
medium
A. head.prev assignment causes error if list has one node
B. No error, code works fine
C. head.next should be assigned to null before head update
D. head should be set to null before updating prev

Solution

  1. Step 1: Check list with one node

    If list has one node, head.next is null after head = head.next.
  2. Step 2: Accessing head.prev causes error

    head is now null, so head.prev causes runtime error (cannot read property 'prev' of null).
  3. Final Answer:

    head.prev assignment causes error if list has one node -> Option A
  4. Quick Check:

    Null head causes error on prev access [OK]
Hint: Check if head is null before accessing head.prev [OK]
Common Mistakes:
  • Not handling single-node list case
  • Accessing properties of null head
  • Assuming list always has multiple nodes
5. You have a doubly linked list with nodes: 5 <-> 15 <-> 25 <-> 35 <-> null. Write a function to delete the first node and return the updated list head. What will be the list after deletion?
hard
A. 15 <-> 25 <-> 35 <-> null
B. 5 <-> 25 <-> 35 <-> null
C. 25 <-> 35 <-> null
D. null

Solution

  1. Step 1: Move head to next node

    Head moves from 5 to 15, the second node.
  2. Step 2: Set new head's prev to null

    Set 15.prev = null to disconnect from old first node.
  3. Step 3: Return updated head

    Return the new head pointing to 15.
  4. Final Answer:

    15 <-> 25 <-> 35 <-> null -> Option A
  5. Quick Check:

    First node removed, list starts at 15 [OK]
Hint: Update head and set new head.prev to null [OK]
Common Mistakes:
  • Not updating prev pointer of new head
  • Returning old head instead of new head
  • Deleting wrong node or skipping nodes
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