Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

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

Choose your learning style10 modes available
Concept Flow - Delete from Beginning of Doubly Linked List
Start at head node
↓
Check if list is empty?
↓
Move head to next node
↓
Update new head's prev to null
↓
Delete old head
↓
Done
Start at the head node, check if list is empty. If not, move head to next node, update new head's prev to null, then delete old head.
Execution Sample
DSA Typescript
class Node {
  data: number;
  prev: Node | null = null;
  next: Node | null = null;
}

function deleteFromBeginning(head: Node | null): Node | null {
  if (!head) return null;
  let newHead = head.next;
  if (newHead) newHead.prev = null;
  return newHead;
}
Deletes the first node of a doubly linked list and returns the new head.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Initial list setup10 <-> 20 <-> 30 <-> nullhead → 10ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:10│←── │ prev:20│←── āˆ… │ next:──┼──→ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
2Check if list is empty10 <-> 20 <-> 30 <-> nullhead is not nullNo change
3Move head to next node20 <-> 30 <-> nullhead → 20ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:10│←── │ prev:20│←── āˆ… │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
4Update new head's prev to null20 <-> 30 <-> null20.prev = nullā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:20│←── āˆ… │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
5Delete old head (10)20 <-> 30 <-> null10 node removedā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:20│←── āˆ… │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
6End20 <-> 30 <-> nullhead points to 20Final list state
šŸ’” head becomes null if list empty; otherwise head moves to next node and old head is deleted
Variable Tracker
VariableStartAfter Step 3After Step 4Final
headNode(10)Node(20)Node(20)Node(20)
head.prevnullNode(10)nullnull
oldHeadNode(10)Node(10)Node(10)deleted
Key Moments - 3 Insights
Why do we set new head's prev to null after moving head?
Because the new head is now the first node, it should not point back to any node. See execution_table step 4 where 20.prev changes from 10 to null.
What happens if the list is empty when we try to delete from beginning?
The function returns null immediately without changes. See execution_table step 2 where head is checked for null.
Why do we delete the old head after moving head pointer?
Because the old head is no longer referenced and should be removed to avoid memory leaks. See execution_table step 5 where node 10 is removed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of head.prev after step 4?
ANode(20)
BNode(10)
Cnull
Dundefined
šŸ’” Hint
Check variable_tracker column 'After Step 4' for head.prev value.
At which step does the head pointer move to the next node?
AStep 2
BStep 3
CStep 4
DStep 5
šŸ’” Hint
Look at execution_table 'Operation' column for 'Move head to next node'.
If the list had only one node, what would be the final value of head after deletion?
Anull
Bundefined
CNode with data
DSame as before deletion
šŸ’” Hint
Refer to concept_flow and execution_table step 2 where empty list is checked.
Concept Snapshot
Delete from Beginning of Doubly Linked List:
- Check if list is empty (head == null)
- Move head to head.next
- Set new head.prev = null if new head exists
- Old head node is removed
- Returns new head or null if list empty
Full Transcript
To delete the first node from a doubly linked list, start by checking if the list is empty. If it is, return null immediately. Otherwise, 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. Finally, remove the old head node to free memory. The new head is returned. This process ensures the list remains connected and correctly linked after deletion.

Practice

(1/5)
1. What happens to the head pointer when you delete the first node from a doubly linked list?
easy
A. It moves to the previous node
B. It becomes null immediately
C. It stays pointing to the deleted node
D. It moves to the next node in the list

Solution

  1. Step 1: Understand the role of head pointer

    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, head must point to the next node to keep the list accessible.
  3. Final Answer:

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

    head points to next node after deletion [OK]
Hint: Head always points to the first valid node [OK]
Common Mistakes:
  • Setting head to null without checking if list is empty
  • Not updating head after deletion
  • Pointing head to deleted node
2. Which of the following is the correct TypeScript syntax to delete the first node from a doubly linked list assuming head is the pointer to the first node?
easy
A. head = head.next; head.prev = null;
B. head.prev = null; head = head.next;
C. head.next = null; head = head.prev;
D. head = null; head.next = null;

Solution

  1. Step 1: Move head to next node

    Assign head to head.next to remove first node reference.
  2. Step 2: Set new head's prev to null

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

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

    Correct syntax to update head and prev pointer [OK]
Hint: Update head first, then fix prev pointer [OK]
Common Mistakes:
  • Setting head.prev before moving head
  • Assigning head.prev to null before head changes
  • Setting head.next to null incorrectly
3. Given the doubly linked list: 10 <-> 20 <-> 30 <-> null, what will be the list after deleting the first node?
medium
A. 20 <-> 30 <-> null
B. 10 <-> 30 <-> null
C. 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 next node

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

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

    First node removed, list starts at 20 [OK]
Hint: After deletion, list starts from second node [OK]
Common Mistakes:
  • Not updating prev pointer of new head
  • Removing wrong node
  • Leaving old head in list
4. Consider this TypeScript code snippet to delete the first node from a doubly linked list:
if (head !== null) {
  head = head.next;
  head.prev = null;
}
What error can occur here?
medium
A. Logical error: head.prev should not be set to null
B. Syntax error due to missing semicolon
C. Runtime error if list has only one node
D. No error, code is correct

Solution

  1. Step 1: Check condition when list has one node

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

    head becomes null, so head.prev causes runtime error (cannot access property of null).
  3. Final Answer:

    Runtime error if list has only one node -> Option C
  4. Quick Check:

    Null pointer access error possible [OK]
Hint: Check if head is null before accessing head.prev [OK]
Common Mistakes:
  • Not checking if head is null after moving to next
  • Assuming list always has multiple nodes
  • Ignoring runtime null errors
5. You have a doubly linked list with nodes: 5 <-> 15 <-> 25 <-> 35 <-> null. Write the correct sequence of operations to delete the first node safely, considering the list might have only one node.
hard
A. head.prev = null; head = head.next;
B. if (head !== null) { head = head.next; if (head !== null) head.prev = null; }
C. if (head.next !== null) { head = head.next; head.prev = null; }
D. head = head.next; head.prev = null;

Solution

  1. Step 1: Check if list is empty

    First, verify head is not null to avoid errors on empty list.
  2. Step 2: Move head to next node and check again

    After moving head, check if new head is not null before setting prev to null.
  3. Final Answer:

    if (head !== null) { head = head.next; if (head !== null) head.prev = null; } -> Option B
  4. Quick Check:

    Safe deletion with null checks [OK]
Hint: Always check for null before accessing pointers [OK]
Common Mistakes:
  • Not checking if head is null after moving
  • Setting prev pointer without null check
  • Assuming list has multiple nodes always
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