Bird
Raised Fist0
DSA Pythonprogramming~10 mins

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

Choose your learning style10 modes available
Concept Flow - Delete from Beginning of Doubly Linked List
Start at head node
↓
Is head null?
Yes→List empty, nothing to delete
No↓
Move head to head.next
↓
Set new head.prev to null
↓
Old head node removed
↓
Done
Start at the head node, check if list is empty. If not, move head to next node and update pointers to remove the first node.
Execution Sample
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

# Delete from beginning
if head is not None:
    head = head.next
    if head is not None:
        head.prev = None
This code deletes the first node of a doubly linked list by moving the head pointer and updating the prev pointer of the new head.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Initial list10 <-> 20 <-> 30 <-> nullhead → node with data 10ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:10│←── │ prev:20│ │ next:──┼──→ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
2Check if head is None10 <-> 20 <-> 30 <-> nullNo changeSame as step 1
3Move head to head.next20 <-> 30 <-> nullhead moves from 10 to 20ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:10│←── │ prev:20│←── │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head (old head 10 detached)
4Set new head.prev to None20 <-> 30 <-> nullhead.prev set from 10 to āˆ…ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:20│←── │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
5Old head node removed20 <-> 30 <-> nullNo pointers to node 10ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:20│←── │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
6End20 <-> 30 <-> nullNo changeFinal list after deletion
šŸ’” Head moved to next node; old head detached and removed from list
Variable Tracker
VariableStartAfter Step 3After Step 4Final
headNode(10)Node(20)Node(20)Node(20)
head.prevNoneNode(10)NoneNone
List size3 nodes3 nodes (old head detached)2 nodes2 nodes
Key Moments - 3 Insights
Why do we set head.prev to None after moving head to head.next?
Because the new head node should not have any node before it. Setting head.prev to None removes the backward link to the old head, as shown in step 4 of the execution_table.
What happens if the list is empty (head is None) when we try to delete?
The code checks if head is None at step 2. If it is None, no deletion happens because there is no node to remove.
Why is the old head node considered removed after moving head?
After step 3, head points to the second node, and no pointers reference the old head node (step 5). This detaches it from the list, effectively deleting it.
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 10
BNode with data 30
CNode with data 20
DNone
šŸ’” Hint
Check the 'Pointer Changes' and 'Visual State' columns at step 3 in execution_table.
At which step does the head.prev pointer get set to None?
AStep 4
BStep 3
CStep 2
DStep 5
šŸ’” Hint
Look at the 'Operation' and 'Pointer Changes' columns in execution_table for when head.prev changes.
If the list initially had only one node, what would be the head after deletion?
ANode with data 10
BNone
CNode with data 20
DNode with data 30
šŸ’” Hint
Think about what happens when head.next is None and head moves to head.next.
Concept Snapshot
Delete from Beginning of Doubly Linked List:
- Check if list is empty (head == None)
- Move head to head.next
- Set new head.prev to None
- Old head node is detached and removed
- List size decreases by one
Full Transcript
To delete the first node of a doubly linked list, start at the head. If the list is empty (head is None), do nothing. Otherwise, move the head pointer to the next node. Then set the new head's prev pointer to None to remove backward link to the old head. The old head node is now detached and removed from the list. This reduces the list size by one. The execution steps show the pointer changes and the visual state of the list after each operation.

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 second node in the list
B. It becomes null immediately
C. It points to the last node in the list
D. It remains pointing to the deleted node

Solution

  1. Step 1: Understand the head pointer role

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

    The head 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 A
  4. Quick Check:

    Head points to new first node after deletion [OK]
Hint: Head always points to the first node, update after deletion [OK]
Common Mistakes:
  • Not updating head pointer after deletion
  • Setting head to null even if list has more nodes
  • Leaving head pointing to deleted node
2. Which of the following is the correct syntax to delete the first node in a doubly linked list in Python?
easy
A. head = None; head.next = None
B. head.prev = None; head = head.next
C. head = head.next; head.prev = None
D. head.next = None; head = head.prev

Solution

  1. Step 1: Move head to next node

    Assign head = head.next to move head forward.
  2. Step 2: Remove backward link

    Set head.prev = None to disconnect previous first node.
  3. Final Answer:

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

    Update head then clear prev link [OK]
Hint: Update head first, then clear prev pointer [OK]
Common Mistakes:
  • Setting prev before moving head
  • Trying to set next of head to None incorrectly
  • Setting head to None without updating
3. What will be the output linked list after deleting the first node from this doubly linked list?
Initial list: 10 <-> 20 <-> 30 <-> null

Code snippet:
head = head.next
head.prev = None
medium
A. 20 <-> 30 <-> null
B. 10 <-> 30 <-> null
C. 30 <-> null
D. null

Solution

  1. Step 1: Move head to second node

    Head moves from 10 to 20.
  2. Step 2: Remove backward link of new head

    Set 20.prev = None, disconnecting 10.
  3. Final Answer:

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

    First node removed, list starts at 20 [OK]
Hint: After deletion, head points to second node [OK]
Common Mistakes:
  • Forgetting to set prev to None
  • Incorrectly skipping nodes
  • Outputting original list without change
4. Identify the error in this code snippet for deleting the first node of a doubly linked list:
if head is not None:
    head = head.next
    head.prev = None
medium
A. No error, code is correct
B. head.prev = None causes error if head.next is None
C. head = head.next should be after setting prev to None
D. Missing deletion of the old head node

Solution

  1. Step 1: Check head.next for None

    If head.next is None, head becomes None after assignment.
  2. Step 2: Setting head.prev causes error

    Trying to set head.prev = None when head is None causes AttributeError.
  3. Final Answer:

    head.prev = None causes error if head.next is None -> Option B
  4. Quick Check:

    Check for None before setting prev [OK]
Hint: Check if head is None before setting head.prev [OK]
Common Mistakes:
  • Not checking if head.next is None
  • Assuming list has more than one node always
  • Ignoring possible AttributeError
5. Given a doubly linked list with nodes: 5 <-> 15 <-> 25 <-> 35 <-> null, after deleting the first node twice, what will be the resulting list?
hard
A. null
B. 15 <-> 25 <-> 35 <-> null
C. 35 <-> null
D. 25 <-> 35 <-> null

Solution

  1. Step 1: First deletion moves head from 5 to 15

    After first deletion, list is 15 <-> 25 <-> 35 <-> null.
  2. Step 2: Second deletion moves head from 15 to 25

    After second deletion, list is 25 <-> 35 <-> null.
  3. Final Answer:

    25 <-> 35 <-> null -> Option D
  4. Quick Check:

    Two deletions remove first two nodes [OK]
Hint: Each deletion moves head to next node [OK]
Common Mistakes:
  • Deleting only once instead of twice
  • Not updating head correctly after each deletion
  • Confusing node values or order
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