Delete Node at End in DSA Python - Time & Space Complexity
When we delete a node at the end of a linked list, we want to know how long it takes as the list grows.
We ask: How does the time to delete the last node change when the list gets bigger?
Analyze the time complexity of the following code snippet.
class Node:
def __init__(self, data):
self.data = data
self.next = None
def delete_at_end(head):
if head is None:
return None
if head.next is None:
return None
current = head
while current.next.next:
current = current.next
current.next = None
return head
This code deletes the last node from a singly linked list by walking through the list to find the second last node.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves through the list nodes.
- How many times: It runs once for each node until the second last node is found.
As the list gets longer, the loop runs more times, roughly one less than the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 9 steps through nodes |
| 100 | About 99 steps through nodes |
| 1000 | About 999 steps through nodes |
Pattern observation: The number of steps grows roughly the same as the list size.
Time Complexity: O(n)
This means the time to delete the last node grows linearly with the list size.
[X] Wrong: "Deleting the last node is always quick and takes constant time."
[OK] Correct: Because we must find the second last node by walking through the list, which takes longer as the list grows.
Understanding how linked list operations scale helps you explain your code clearly and shows you know how data size affects performance.
"What if we had a pointer to the tail node? How would the time complexity change when deleting the last node?"
Practice
Delete Node at End operation in a singly linked list?Solution
Step 1: Understand the operation
The operation aims to remove a node from the end of the singly linked list.Step 2: Identify the correct description
Removing the last node means deleting the tail node, not adding or reversing nodes.Final Answer:
Remove the last node from the linked list -> Option CQuick Check:
Delete Node at End = Remove last node [OK]
- Confusing delete at end with delete at beginning
- Thinking it adds a node instead of removing
- Mixing up with reversing the list
Solution
Step 1: Understand deletion at end
To delete the last node, we must find the second last node and make its next pointer null.Step 2: Check each option
Setting head = null deletes the whole list, not just last node. Setting last node's next to head creates a cycle. Creating a new node and assigning to head adds a node, not deletes.Final Answer:
Traverse to second last node and set its next to null -> Option DQuick Check:
Delete last node = second last node next = null [OK]
- Setting head to null deletes entire list
- Creating cycles by wrong pointer assignments
- Adding nodes instead of deleting
1 -> 2 -> 3 -> 4 -> null?Solution
Step 1: Identify the last node
The last node in the list is the node with value 4.Step 2: Delete the last node
Removing node 4 means the new last node is 3, so the list ends at 3 with next as null.Final Answer:
1 -> 2 -> 3 -> null -> Option AQuick Check:
Last node 4 removed = 1 -> 2 -> 3 [OK]
- Not updating second last node's next to null
- Removing wrong node (like first node)
- Confusing output with original list
def delete_end(head):
if head is None:
return None
if head.next is None:
return None
temp = head
while temp.next is not None:
temp = temp.next
temp.next = None
return head
What is the error in this code?Solution
Step 1: Analyze the loop condition
The loop runs while temp.next is not null, so temp moves to the last node.Step 2: Identify pointer update mistake
After loop, temp points to last node, but to delete last node, we need second last node to set its next to null. Here, temp.next = null sets last node's next to null, which is already null, so no deletion happens.Final Answer:
temp.next is null check misses second last node -> Option BQuick Check:
Must stop at second last node to delete last [OK]
- Stopping loop at last node instead of second last
- Not handling single node list correctly
- Setting wrong pointers causing no deletion
10 -> 20 -> 30 -> 40 -> 50 -> null, write the steps to delete the last node and then add a new node with value 60 at the end. What will be the final list?Solution
Step 1: Delete the last node (50)
Traverse to second last node (40) and set its next to null, removing 50.Step 2: Add new node with value 60 at end
Traverse to last node (40) and set its next to new node with value 60, new node's next is null.Final Answer:
10 -> 20 -> 30 -> 40 -> 60 -> null -> Option AQuick Check:
Delete last 50, add 60 at end = 10->20->30->40->60 [OK]
- Not updating second last node's next to null before adding
- Appending new node without deleting last node
- Confusing node values or order
Ready to prove it?
Pass the test and this pattern moves unknown
