Delete Node at End in DSA C++ - Time & Space Complexity
We want to understand how long it takes to remove the last node from a linked list.
How does the time needed change as the list gets bigger?
Analyze the time complexity of the following code snippet.
void deleteAtEnd(Node*& head) {
if (head == nullptr) return;
if (head->next == nullptr) {
delete head;
head = nullptr;
return;
}
Node* temp = head;
while (temp->next->next != nullptr) {
temp = temp->next;
}
delete temp->next;
temp->next = nullptr;
}
This code removes 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 nodes to find the second last node.
- How many times: It runs once for each node except the last two, so roughly n-2 times for a list of size n.
As the list gets longer, the loop runs more times, almost one time per node.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 8 steps |
| 100 | About 98 steps |
| 1000 | About 998 steps |
Pattern observation: The number of steps grows roughly in a straight line with the size of the list.
Time Complexity: O(n)
This means the time to delete the last node grows directly with the number of nodes in the list.
[X] Wrong: "Deleting the last node is always quick because it's just one node."
[OK] Correct: You must find the node before the last one, which takes time proportional to the list size.
Understanding this helps you explain how linked lists work and why some operations take longer than others.
"What if the list had a tail pointer that points directly to the last node? How would the time complexity change?"
Practice
Solution
Step 1: Understand the linked list structure
Each node points to the next node. The last node points to null.Step 2: Effect of deleting the last node
Deleting the last node means the previous node's next pointer must be set to null to mark new end.Final Answer:
The second last node's next pointer becomes null -> Option AQuick Check:
Last node removed, previous node points to null [OK]
- Setting head to null incorrectly
- Deleting the first node instead
- Not updating the previous node's next pointer
Solution
Step 1: Identify the last node's pointer
In singly linked list, last node's previous node points to it via next pointer.Step 2: Delete last node and update pointer
Deleting lastNode->next and setting lastNode->next to nullptr correctly removes last node.Final Answer:
delete lastNode->next; lastNode->next = nullptr; -> Option DQuick Check:
Delete next node and set pointer null [OK]
- Deleting head instead of last node
- Using prev pointer in singly linked list
- Not setting pointer to nullptr after delete
Solution
Step 1: Identify the last node
The last node is 40, which points to null.Step 2: Remove last node and update previous node
Deleting 40 means 30's next pointer becomes null, ending list at 30.Final Answer:
10 -> 20 -> 30 -> null -> Option BQuick Check:
Last node 40 removed, list ends at 30 [OK]
- Not updating previous node's next pointer
- Deleting head node by mistake
- Outputting original list without deletion
struct Node {
int data;
Node* next;
};
void deleteLast(Node*& head) {
if (head == nullptr) return;
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
delete temp;
temp = nullptr;
}Solution
Step 1: Analyze traversal to last node
The loop moves temp to last node, but does not track previous node.Step 2: Identify missing pointer update
Deleting temp frees last node but previous node's next still points to freed memory, causing dangling pointer.Final Answer:
Does not update previous node's next pointer to null -> Option CQuick Check:
Previous node's next must be null after deletion [OK]
- Not tracking previous node in traversal
- Deleting node without updating links
- Setting temp to nullptr does not fix list
Solution
Step 1: Identify single node list condition
Only one node means head points to that node and node's next is null.Step 2: Delete node and update head
Deleting the only node requires setting head to null to mark empty list.Final Answer:
Set head pointer to null -> Option AQuick Check:
Single node deleted means empty list head = null [OK]
- Not setting head to null after deletion
- Trying to update next pointer of deleted node
- Leaving dangling pointer in head
Ready to prove it?
Pass the test and this pattern moves unknown
