Delete from Beginning of Doubly Linked List in DSA Python - Time & Space Complexity
When we delete the first node from a doubly linked list, we want to know how the time needed changes as the list grows.
We ask: How much work does it take to remove the first item when the list is very long?
Analyze the time complexity of the following code snippet.
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
class DoublyLinkedList:
def __init__(self):
self.head = None
def delete_from_beginning(self):
if not self.head:
return # List is empty
self.head = self.head.next
if self.head:
self.head.prev = None
This code removes the first node from a doubly linked list by updating the head pointer and adjusting links.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Updating pointers of the first node and head reference.
- How many times: This happens only once per deletion, no loops or recursion involved.
Deleting the first node always takes the same steps, no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 2-3 pointer updates |
| 100 | 2-3 pointer updates |
| 1000 | 2-3 pointer updates |
Pattern observation: The number of steps stays the same no matter how large the list is.
Time Complexity: O(1)
This means deleting the first node takes a fixed amount of time regardless of list size.
[X] Wrong: "Deleting the first node takes longer if the list is bigger because we have to check all nodes."
[OK] Correct: We only change the head and its immediate neighbors, so the list size does not affect the steps needed.
Knowing that deleting from the start of a doubly linked list is quick helps you explain efficient list operations clearly in interviews.
"What if we changed this to delete from the end of the doubly linked list? How would the time complexity change?"
Practice
head pointer when you delete the first node from a doubly linked list?Solution
Step 1: Understand the head pointer role
The head pointer always points to the first node in the list.Step 2: After deleting the first node
The head must move to the next node, which becomes the new first node.Final Answer:
It moves to the second node in the list -> Option AQuick Check:
Head points to new first node after deletion [OK]
- Not updating head pointer after deletion
- Setting head to null even if list has more nodes
- Leaving head pointing to deleted node
Solution
Step 1: Move head to next node
Assignhead = head.nextto move head forward.Step 2: Remove backward link
Sethead.prev = Noneto disconnect previous first node.Final Answer:
head = head.next; head.prev = None -> Option CQuick Check:
Update head then clear prev link [OK]
- Setting prev before moving head
- Trying to set next of head to None incorrectly
- Setting head to None without updating
Initial list: 10 <-> 20 <-> 30 <-> null
Code snippet:
head = head.next head.prev = None
Solution
Step 1: Move head to second node
Head moves from 10 to 20.Step 2: Remove backward link of new head
Set 20.prev = None, disconnecting 10.Final Answer:
20 <-> 30 <-> null -> Option AQuick Check:
First node removed, list starts at 20 [OK]
- Forgetting to set prev to None
- Incorrectly skipping nodes
- Outputting original list without change
if head is not None:
head = head.next
head.prev = None
Solution
Step 1: Check head.next for None
If head.next is None, head becomes None after assignment.Step 2: Setting head.prev causes error
Trying to sethead.prev = Nonewhen head is None causes AttributeError.Final Answer:
head.prev = None causes error if head.next is None -> Option BQuick Check:
Check for None before setting prev [OK]
- Not checking if head.next is None
- Assuming list has more than one node always
- Ignoring possible AttributeError
Solution
Step 1: First deletion moves head from 5 to 15
After first deletion, list is 15 <-> 25 <-> 35 <-> null.Step 2: Second deletion moves head from 15 to 25
After second deletion, list is 25 <-> 35 <-> null.Final Answer:
25 <-> 35 <-> null -> Option DQuick Check:
Two deletions remove first two nodes [OK]
- Deleting only once instead of twice
- Not updating head correctly after each deletion
- Confusing node values or order
Ready to prove it?
Pass the test and this pattern moves unknown
