Delete Node at End in DSA C - Time & Space Complexity
When we delete a node at the end of a linked list, we want to know how the time taken changes as the list grows.
We ask: How much work does the computer do when the list gets bigger?
Analyze the time complexity of the following code snippet.
void deleteAtEnd(Node** head) {
if (*head == NULL) return;
if ((*head)->next == NULL) {
free(*head);
*head = NULL;
return;
}
Node* temp = *head;
while (temp->next->next != NULL) {
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
}
This code deletes the last node of a singly linked list by traversing to the second last node and removing the last.
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, so roughly n-1 times for n nodes.
As the list gets longer, the loop runs more times, almost once per node except the last.
| 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 work grows roughly in a straight line with the number of nodes.
Time Complexity: O(n)
This means the time to delete the last node grows directly with the list size.
[X] Wrong: "Deleting the last node is always quick because it's at the end."
[OK] Correct: We must find the node before the last, which takes time proportional to the list length.
Understanding this helps you explain how linked lists work and why some operations take longer as data grows.
"What if we kept a pointer to the last 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's goal
The operation aims to remove the last node in the linked list, which is the node that points to null.Step 2: Identify the correct description
Removing the last node means deleting the node at the end, not adding or reversing nodes.Final Answer:
To remove the last node from the linked list -> Option DQuick Check:
Delete Node at End = Remove last node [OK]
- Confusing delete at end with delete at beginning
- Thinking it adds a node instead of deleting
- Mixing up with reversing the list
head?Solution
Step 1: Check for empty or single-node list and traverse to second last node
if (head == NULL || head->next == NULL) return; Node* temp = head; while (temp->next->next != NULL) temp = temp->next; free(temp->next); temp->next = NULL; checks if list is empty or single node, then moves to the node before last usingtemp->next->next != NULL.Step 2: Free last node and update pointer
It frees the last node and setstemp->nextto NULL, correctly deleting the last node.Final Answer:
if (head == NULL || head->next == NULL) return; Node* temp = head; while (temp->next->next != NULL) temp = temp->next; free(temp->next); temp->next = NULL; -> Option AQuick Check:
Traverse to second last, free last, set next NULL = if (head == NULL || head->next == NULL) return; Node* temp = head; while (temp->next->next != NULL) temp = temp->next; free(temp->next); temp->next = NULL; [OK]
- Freeing node without updating previous node's next pointer
- Not checking for empty list
- Freeing head without traversal
Solution
Step 1: Identify the last node
The last node is 40, which points to NULL.Step 2: Remove the last node and update the second last node's next pointer
After deletion, 30 becomes the last node and its next pointer is set to NULL.Final Answer:
10 -> 20 -> 30 -> NULL -> Option BQuick Check:
Last node 40 removed = 10 -> 20 -> 30 -> NULL [OK]
- Not updating second last node's next pointer
- Deleting wrong node (like first node)
- Output list unchanged
void deleteEnd(Node** head) {
if (*head == NULL) return;
Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
free(temp);
temp = NULL;
}Solution
Step 1: Analyze traversal logic
The loop moves temp to the last node (where temp->next is NULL).Step 2: Check pointer update after freeing
It frees the last node but does not update the second last node's next pointer to NULL, leaving a dangling pointer.Final Answer:
It does not update the second last node's next pointer to NULL -> Option AQuick Check:
Missing next pointer update causes dangling pointer [OK]
- Freeing node without updating previous node's next
- Not handling empty list
- Incorrect loop condition causing crash
Solution
Step 1: Delete the last node (45)
Traverse to second last node (35), free last node (45), set 35's next to NULL.Step 2: Insert new node with value 50 at end
Traverse to last node (35), create new node with 50, set 35's next to new node, new node's next to NULL.Final Answer:
5 -> 15 -> 25 -> 35 -> 50 -> NULL -> Option CQuick Check:
Delete last, then add 50 at end = 5 -> 15 -> 25 -> 35 -> 50 -> NULL [OK]
- Not updating pointers correctly after deletion
- Appending new node without deleting last
- Inserting new node at wrong position
Ready to prove it?
Pass the test and this pattern moves unknown
