Delete Node by Value in DSA C++ - Time & Space Complexity
We want to know how long it takes to remove a node with a specific value 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 deleteNodeByValue(Node*& head, int value) {
if (!head) return;
if (head->data == value) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* current = head;
while (current->next && current->next->data != value) {
current = current->next;
}
if (current->next) {
Node* temp = current->next;
current->next = current->next->next;
delete temp;
}
}
This code searches for the first node with the 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 grows, the time to find the node grows roughly in a straight line.
| 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 directly with 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 each 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 for deleting a node by value change?"
Practice
Delete Node by Value operation in a singly 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 Remove the first node that contains the given value from the list, while others describe different operations.Final Answer:
Remove the first node that contains the given value from the list -> Option CQuick Check:
Delete node by value means removing matching node [OK]
- Confusing delete with add or search operations
- Thinking it reverses the list
- Assuming it deletes all nodes with the value
Solution
Step 1: Check parameter order and return type
The function should take the head pointer first and the value to delete second, returning void since it modifies the list.Step 2: Match with options
void deleteNode(Node* head, int val) { /* code */ } matches the correct parameter order and return type. Others have wrong order or return types.Final Answer:
void deleteNode(Node* head, int val) { /* code */ } -> Option DQuick Check:
Head pointer first, value second, void return [OK]
- Swapping parameter order
- Using wrong return type
- Returning Node instead of void
void deleteNode(Node*& head, int val) {
if (!head) return;
if (head->data == val) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* curr = head;
while (curr->next && curr->next->data != val) {
curr = curr->next;
}
if (curr->next) {
Node* temp = curr->next;
curr->next = curr->next->next;
delete temp;
}
}Solution
Step 1: Check if head node has value 3
Head node is 1, not 3, so skip first if block.Step 2: Traverse to find first node with value 3
Loop stops at node before first 3 (which is node with value 1). Then deletes first 3 node.Step 3: Update links after deletion
After deletion, list becomes 1 -> 5 -> 3 -> 7 -> null.Final Answer:
1 -> 5 -> 3 -> 7 -> null -> Option AQuick Check:
First 3 deleted, rest unchanged [OK]
- Deleting all nodes with value 3
- Deleting wrong node (head vs next)
- Not updating links correctly
void deleteNode(Node* head, int val) {
if (!head) return;
if (head->data == val) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* curr = head;
while (curr->next && curr->next->data != val) {
curr = curr->next;
}
if (curr->next) {
Node* temp = curr->next;
curr->next = curr->next->next;
delete temp;
}
}Solution
Step 1: Analyze head pointer parameter
Head is passed by value, so changing head inside function does not affect original list.Step 2: Understand impact on deleting head node
Deleting head node updates local head only, original list head remains unchanged, causing bug.Final Answer:
The head pointer is passed by value, so changes to head are lost -> Option BQuick Check:
Pass head by reference to update original list [OK]
- Not passing head by reference
- Assuming head changes reflect outside function
- Ignoring memory leaks
deleteNode function to delete all nodes with a given value instead of just the first one?
Choose the best approach.Solution
Step 1: Understand requirement to delete all matching nodes
We must traverse entire list and remove every node with the given value.Step 2: Choose approach that deletes all nodes in one pass
Use a loop to traverse the list and delete every node matching the value, updating links each time uses a loop to check and delete nodes as we go, updating links properly.Step 3: Evaluate other options
Call the existing deleteNode function repeatedly until no node with the value remains is inefficient and error-prone; C and D do not delete all nodes.Final Answer:
Use a loop to traverse the list and delete every node matching the value, updating links each time -> Option AQuick Check:
Loop and delete all matching nodes in one traversal [OK]
- Deleting only first occurrence
- Calling delete repeatedly without updating head
- Ignoring link updates after deletion
Ready to prove it?
Pass the test and this pattern moves unknown
