Delete from End of Doubly Linked List in DSA C - Time & Space Complexity
We want to understand how long it takes to remove the last item from a doubly linked list.
How does the time needed change as the list gets bigger?
Analyze the time complexity of the following code snippet.
// Delete node from end of doubly linked list
void deleteEnd(Node** head) {
if (*head == NULL) return;
Node* temp = *head;
if (temp->next == NULL) {
free(temp);
*head = NULL;
return;
}
while (temp->next != NULL) {
temp = temp->next;
}
temp->prev->next = NULL;
free(temp);
}
This code removes the last node from a doubly linked list by walking to the end and unlinking it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves through nodes to find the last one.
- How many times: It runs once for each node until the end, so as many times as the list length.
As the list gets longer, the time to reach the last node grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps to reach the end |
| 100 | About 100 steps to reach the end |
| 1000 | About 1000 steps to reach the end |
Pattern observation: The steps increase directly with the number of nodes.
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: Without a pointer to the last node, we must walk through the whole list, so time grows with list length.
Knowing how linked list operations scale helps you explain your code choices clearly and confidently.
"What if we kept a pointer to the last node? How would the time complexity change?"
Practice
tail pointer when you delete the last node from a doubly linked list?Solution
Step 1: Understand the tail pointer role
The tail pointer always points to the last node in the doubly linked list.Step 2: Update tail after deletion
When the last node is deleted, the tail pointer must move to the previous node to keep pointing to the new last node.Final Answer:
It moves to the previous node of the deleted node. -> Option DQuick Check:
Tail points to previous node after deletion [OK]
- Setting tail to NULL without checking if list is empty
- Not updating tail pointer after deletion
- Pointing tail to head incorrectly
Solution
Step 1: Free the last node correctly
We first move tail to previous node, then free the old last node which is tail->next.Step 2: Update tail's next pointer
After freeing, set tail->next to NULL to mark new end of list.Final Answer:
tail = tail->prev; free(tail->next); tail->next = NULL; -> Option AQuick Check:
Move tail first, free old node, update next pointer [OK]
- Freeing tail before moving pointer
- Not setting tail->next to NULL
- Incorrect pointer assignments causing memory errors
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
Node* tail = NULL;
// List: 10 <-> 20 <-> 30
// tail points to node with data 30
// Delete last node
Node* temp = tail;
tail = tail->prev;
free(temp);
tail->next = NULL;
// Print list from head
Node* head = tail;
while(head->prev) head = head->prev;
while(head) {
printf("%d ", head->data);
head = head->next;
}Solution
Step 1: Understand list before deletion
Initial list is 10 <-> 20 <-> 30, tail points to 30.Step 2: Delete last node and update pointers
Last node 30 is freed, tail moves to 20, tail->next set to NULL.Step 3: Print list from head
Head is found by moving to start (10), printing nodes 10 and 20.Final Answer:
10 20 -> Option AQuick Check:
Last node removed, list prints 10 20 [OK]
- Printing old tail node after deletion
- Not updating tail->next to NULL
- Starting print from tail instead of head
void deleteEnd(Node** tail) {
if (*tail == NULL) return;
Node* temp = *tail;
*tail = (*tail)->prev;
free(temp);
(*tail)->next = NULL;
}Solution
Step 1: Check for empty list after deletion
If the list had only one node, after deletion tail becomes NULL, so (*tail)->next causes error.Step 2: Fix by checking tail before dereference
Must check if *tail is not NULL before setting (*tail)->next = NULL.Final Answer:
Dereferencing tail without checking if list becomes empty. -> Option BQuick Check:
Check tail NULL before accessing next pointer [OK]
- Dereferencing NULL pointer after deletion
- Ignoring empty list edge case
- Not freeing memory causing leaks
// Initial tail points to 35
// Delete last node twice
Node* temp;
temp = tail;
tail = tail->prev;
free(temp);
tail->next = NULL;
temp = tail;
tail = tail->prev;
free(temp);
tail->next = NULL;
// Print from head
Node* head = tail;
while(head->prev) head = head->prev;
while(head) {
printf("%d ", head->data);
head = head->next;
}Solution
Step 1: Understand initial list and tail
List is 5 <-> 15 <-> 25 <-> 35, tail points to 35.Step 2: Delete last node once
Delete 35, tail moves to 25, tail->next = NULL.Step 3: Delete last node second time
Delete 25, tail moves to 15, tail->next = NULL.Step 4: Print list from head
Head found by moving to start (5), print nodes 5 and 15.Final Answer:
5 15 -> Option CQuick Check:
Two deletions remove last two nodes, list prints 5 15 [OK]
- Not updating tail->next to NULL after each deletion
- Printing from tail instead of head
- Assuming deleted nodes still exist
Ready to prove it?
Pass the test and this pattern moves unknown
