Delete by Value in Doubly Linked List in DSA C++ - Time & Space Complexity
We want to know how long it takes to remove a node by its value 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.
void deleteByValue(Node*& head, int value) {
Node* current = head;
while (current != nullptr && current->data != value) {
current = current->next;
}
if (current == nullptr) return; // value not found
if (current->prev != nullptr) {
current->prev->next = current->next;
} else {
head = current->next;
}
if (current->next != nullptr) {
current->next->prev = current->prev;
}
delete current;
}
This code searches for the first node with the given value and removes it from the doubly linked list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves through nodes to find the value.
- How many times: Up to n times, where n is the number of nodes in the list.
As the list grows, the search may take longer because it might 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 by value 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 one node."
[OK] Correct: We must first find the node by checking nodes one by one, which can take time proportional to the list size.
Understanding this helps you explain how searching affects deletion time in linked lists, a common topic in coding interviews.
"What if the list was sorted? How would that change the time complexity of deleting by value?"
Practice
Solution
Step 1: Locate the node with the given value
Traverse the list from head to tail to find the node whose data matches the value to delete.Step 2: Update links to remove the node
Adjust the previous node's next pointer and the next node's previous pointer to bypass the node to be deleted.Final Answer:
The node with the matching value is removed and links are updated. -> Option BQuick Check:
Delete by value removes matching node [OK]
- Not updating both previous and next pointers
- Deleting node without checking value
- Deleting all nodes instead of one
Solution
Step 1: Understand parameter passing for head pointer
Head pointer must be passed by reference to allow modification if the first node is deleted.Step 2: Check function return type
Deletion functions usually return void as they modify the list in place.Final Answer:
void deleteNode(Node* &head, int val) { /* code */ } -> Option AQuick Check:
Pass head by reference and return void [OK]
- Passing head by value causing no list update
- Returning Node instead of void
- Missing head pointer parameter
struct Node {
int data;
Node* prev;
Node* next;
};
// List: 10 <-> 20 <-> 30
// deleteNode(head, 20);
// printList(head);
Solution
Step 1: Identify node to delete
Node with value 20 is in the middle of the list 10 <-> 20 <-> 30.Step 2: Update links after deletion
After deleting 20, 10's next points to 30, and 30's prev points to 10.Final Answer:
10 <-> 30 <-> null -> Option DQuick Check:
Middle node deleted, links updated [OK]
- Not updating previous pointer of next node
- Not updating next pointer of previous node
- Leaving deleted node linked
void deleteNode(Node* &head, int val) {
Node* temp = head;
while (temp != nullptr && temp->data != val) {
temp = temp->next;
}
if (temp == nullptr) return;
if (temp->prev != nullptr) temp->prev->next = temp->next;
if (temp->next != nullptr) temp->next->prev = temp->prev;
delete temp;
}Solution
Step 1: Check head update when deleting first node
If the node to delete is the head, head pointer must be updated to next node.Step 2: Verify code for head update
Code does not update head pointer when temp->prev is nullptr (first node).Final Answer:
Does not update head if first node is deleted -> Option CQuick Check:
Head update missing for first node deletion [OK]
- Forgetting to update head pointer
- Not handling empty list
- Deleting without checking node existence
Solution
Step 1: Delete node with value 5 (head node)
Update head to 10, set 10's prev to nullptr. List becomes 10 <-> 15 <-> 20.Step 2: Delete node with value 20 (tail node)
Update 15's next to nullptr. List becomes 10 <-> 15.Final Answer:
10 <-> 15 <-> null -> Option AQuick Check:
Deleted head and tail nodes correctly [OK]
- Not updating head when deleting first node
- Not updating tail when deleting last node
- Leaving dangling pointers
Ready to prove it?
Pass the test and this pattern moves unknown
