Delete Node by Value in DSA C - Time & Space Complexity
We want to know how the time needed to delete a node by value changes as the list grows.
How does the number of steps grow when the list gets bigger?
Analyze the time complexity of the following code snippet.
void deleteNodeByValue(Node** head, int value) {
Node* temp = *head;
Node* prev = NULL;
while (temp != NULL && temp->data != value) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return; // value not found
if (prev == NULL) *head = temp->next; // delete head
else prev->next = temp->next;
free(temp);
}
This code searches for a node with a given value and removes it from the linked list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves through the list nodes one by one.
- How many times: Up to n times, where n is the number of nodes in the list.
As the list gets longer, the search takes more steps because it may check more nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The number of steps grows roughly in direct proportion to the list size.
Time Complexity: O(n)
This means the time to delete a node grows linearly with the number of nodes in the list.
[X] Wrong: "Deleting a node by value always takes constant time because we just remove it."
[OK] Correct: We must first find the node by checking nodes one by one, which can take time proportional to the list size.
Understanding how searching affects deletion time helps you explain linked list operations clearly and confidently.
"What if the list was doubly linked? How would the time complexity of deleting a node by value change?"
Practice
Delete Node by Value operation in a linked list?Solution
Step 1: Understand the operation's goal
The operation aims to remove a node that matches a specific value from the linked list.Step 2: Identify the correct description
Removing the first node with the given value matches To remove the first node that contains the given value from the list, while others describe different operations.Final Answer:
To remove the first node that contains the given value from the list -> Option AQuick Check:
Delete Node by Value = Remove matching node [OK]
- Confusing deletion by value with insertion
- Thinking it reverses the list
- Assuming it finds position only
Solution
Step 1: Understand deletion start
We must check if the head node itself contains the value to delete before moving forward.Step 2: Identify correct approach
Setting a pointer to head and checking its value is the correct first step; other options are incorrect or irrelevant.Final Answer:
Set a pointer to the head and check if head's value matches -> Option DQuick Check:
Start at head to delete by value [OK]
- Freeing head without checking value
- Creating new nodes unnecessarily
- Reversing list before deletion
Solution
Step 1: Locate node with value 30
Traverse the list: 10, 20, then 30 is found as the third node.Step 2: Remove node 30 and link previous to next
Link node with value 20 directly to node with value 40, skipping 30.Final Answer:
10 -> 20 -> 40 -> NULL -> Option BQuick Check:
Deleted 30, list skips it [OK]
- Not updating previous node's next pointer
- Deleting wrong node
- Leaving deleted node in list
void deleteNode(Node** head, int val) {
Node* temp = *head;
Node* prev = NULL;
while (temp != NULL && temp->data != val) {
prev = temp;
temp = temp->next;
}
free(temp);
if (prev != NULL) {
prev->next = temp->next;
} else {
*head = temp->next;
}
}Solution
Step 1: Analyze order of free and pointer update
The code frees temp before updating prev->next or head, so temp->next access is invalid.Step 2: Identify correct fix
Pointers must be updated before freeing the node to avoid accessing freed memory.Final Answer:
free(temp) is called before updating pointers, causing invalid access -> Option CQuick Check:
Free after pointer update to avoid errors [OK]
- Freeing node before fixing links
- Ignoring NULL list case
- Not updating prev pointer
Solution
Step 1: Identify nodes with value > 15
Nodes with values 20 and 25 are greater than 15 and must be deleted.Step 2: Delete nodes and link remaining nodes
After removing 20 and 25, the list becomes 5 -> 10 -> 15 -> NULL.Final Answer:
5 -> 10 -> 15 -> NULL -> Option AQuick Check:
Removed nodes > 15, list ends at 15 [OK]
- Deleting wrong nodes
- Not updating links after deletion
- Assuming only one node deleted
Ready to prove it?
Pass the test and this pattern moves unknown
