Delete Node at Beginning in DSA Python - Time & Space Complexity
When we delete a node at the beginning of a linked list, we want to know how the time it takes changes as the list grows.
We ask: Does deleting the first node take longer if the list is bigger?
Analyze the time complexity of the following code snippet.
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def delete_at_beginning(self):
if self.head is not None:
self.head = self.head.next
This code removes the first node by moving the head pointer to the next node.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Updating the head pointer to the next node.
- How many times: This happens only once per deletion, no loops or repeated steps.
Deleting the first node always takes the same steps, no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The work stays the same even if the list grows larger.
Time Complexity: O(1)
This means deleting the first node takes the same amount of time no matter how big the list is.
[X] Wrong: "Deleting the first node takes longer if the list is very big because we have to check all nodes."
[OK] Correct: We only change the head pointer once; we do not look at other nodes, so size does not affect the time.
Understanding this simple operation helps build confidence in working with linked lists and shows you can reason about how changes affect performance.
"What if we changed the code to delete the last node instead? How would the time complexity change?"
Practice
Solution
Step 1: Understand the head pointer role
The head pointer points to the first node in the list.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.Final Answer:
It moves to the next node in the list -> Option DQuick Check:
Head points to next node after deletion [OK]
- Thinking head becomes null even if list has more nodes
- Assuming head points to last node after deletion
- Not updating head pointer at all
head?Solution
Step 1: Understand Python linked list node deletion
To delete the first node, we move the head pointer to the next node.Step 2: Check syntax correctness
Assigninghead = head.nextcorrectly moves head to the next node. Other options are invalid Python or incorrect logic.Final Answer:
head = head.next -> Option AQuick Check:
Assign head to head.next to delete first node [OK]
- Using 'null' instead of 'None' in Python
- Trying to delete node with 'delete' keyword (not valid in Python)
- Assigning head.next = head which breaks list
1 -> 2 -> 3 -> 4 -> null?Solution
Step 1: Identify the first node to delete
The first node has value 1.Step 2: Delete first node and update head
After deletion, head points to node with value 2, so list becomes 2 -> 3 -> 4 -> null.Final Answer:
2 -> 3 -> 4 -> null -> Option CQuick Check:
First node removed, list starts at 2 [OK]
- Removing wrong node (like second node)
- Not updating head pointer
- Assuming list becomes empty
class Node:
def __init__(self, data):
self.data = data
self.next = None
head = Node(10)
head.next = Node(20)
head = head.next.next # Delete first node
Solution
Step 1: Trace the assignment head = head.next.next
head.next is node with data 20, head.next.next is None (end of list).Step 2: Effect of assignment
Assigning head to None loses the entire list, effectively emptying it.Final Answer:
head becomes None causing loss of list -> Option AQuick Check:
head = head.next.next sets head to None [OK]
- Assigning head to head.next.next instead of head.next
- Expecting list to remain intact after wrong assignment
- Ignoring None assignment effect
5 -> 10 -> 15 -> 20 -> null. Write the steps to delete the first node and then insert a new node with value 25 at the beginning. What will be the final list?Solution
Step 1: Delete the first node
Move head pointer from node 5 to node 10, so list becomes 10 -> 15 -> 20 -> null.Step 2: Insert new node with value 25 at beginning
Create new node with data 25, set its next to current head (node 10), then update head to new node.Step 3: Final list after insertion
List becomes 25 -> 10 -> 15 -> 20 -> null.Final Answer:
25 -> 10 -> 15 -> 20 -> null -> Option BQuick Check:
Delete first, then add new node at head [OK]
- Not updating head after deletion
- Appending new node at end instead of beginning
- Forgetting to link new node to old head
Ready to prove it?
Pass the test and this pattern moves unknown
