Delete by Value in Doubly Linked List in DSA C# - Time & Space Complexity
We want to understand how long it takes to delete a node by value in a doubly linked list.
How does the time needed change as the list gets bigger?
Analyze the time complexity of the following code snippet.
public void DeleteByValue(int value) {
Node current = head;
while (current != null) {
if (current.Data == value) {
if (current.Prev != null) current.Prev.Next = current.Next;
else head = current.Next;
if (current.Next != null) current.Next.Prev = current.Prev;
return;
}
current = current.Next;
}
}
This code searches the list for the first node with the given value and removes it by adjusting links.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing the list nodes one by one 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 time to find the value grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 node checks |
| 100 | Up to 100 node checks |
| 1000 | Up to 1000 node checks |
Pattern observation: The number of steps grows linearly with the list size.
Time Complexity: O(n)
This means the time to delete by value grows linearly as the list gets longer.
[X] Wrong: "Deleting by value is always fast because we just remove the node."
[OK] Correct: We must first find the node by checking each one, which can take time proportional to the list size.
Understanding this helps you explain how searching affects deletion speed in linked lists, a common topic in interviews.
"What if the list was sorted? How would that change the time complexity of deleting by value?"
Practice
Solution
Step 1: Search for the node with the given value
Traverse the list from head to tail to find a node matching the value.Step 2: Check if node is found
If no node matches the value, no deletion occurs and the list stays the same.Final Answer:
The list remains unchanged because no node matches the value. -> Option DQuick Check:
Delete by value with no match = no change [OK]
- Assuming deletion happens even if value not found
- Trying to delete head without checking value
- Expecting an error instead of no action
Solution
Step 1: Understand pointers in doubly linked list
When deleting a node, the next node's previous pointer must point to the deleted node's previous node.Step 2: Correct pointer update
Setting nodeToDelete.Next.Prev = nodeToDelete.Prev correctly links the next node back to the previous node.Final Answer:
nodeToDelete.Next.Prev = nodeToDelete.Prev; -> Option AQuick Check:
Update next node's Prev to deleted node's Prev [OK]
- Mixing up Next and Prev pointers
- Assigning Prev to Next's Prev incorrectly
- Not updating both Next and Prev pointers
Solution
Step 1: Locate node with value 30
Traverse the list and find the node containing 30 between 20 and 40.Step 2: Remove node 30 and update pointers
Set 20's Next to 40 and 40's Prev to 20, skipping 30.Final Answer:
10 <-> 20 <-> 40 <-> null -> Option BQuick Check:
Deleted 30, linked 20 and 40 directly [OK]
- Not updating both Prev and Next pointers
- Leaving deleted node in the list
- Deleting wrong node by mistake
if (current == null) return; if (current.Prev != null) current.Prev.Next = current.Next; if (current.Next != null) current.Next.Prev = current.Prev; if (current == head) head = current.Next;What is the bug in this code?
Solution
Step 1: Verify null safety
The initial check if (current == null) return; and conditional pointer accesses prevent null references.Step 2: Check head and pointer updates
Head is updated correctly when deleting the first node, and pointers are linked properly for middle nodes.Step 3: Identify the issue with last node
The code sets current.Prev.Next = null but does not update a potential tail pointer to current.Prev, leaving the tail pointing to the deleted node.Final Answer:
It does not handle deleting the last node correctly. -> Option CQuick Check:
Missing tail update for last node deletion [OK]
- Ignoring null checks before pointer access
- Not updating head when deleting first node
- Assuming garbage collection removes node immediately
Solution
Step 1: Traverse list from head to tail
Visit each node and check if its value is greater than 15.Step 2: Delete matching nodes and update pointers
For each node with value > 15, remove it by linking its previous node to its next node, updating both Next and Prev pointers.Step 3: Continue traversal carefully
After deletion, continue traversal without skipping nodes to delete all matching nodes.Final Answer:
Traverse from head, delete nodes with value > 15, updating pointers carefully. -> Option AQuick Check:
Delete all matching nodes with pointer updates [OK]
- Deleting nodes without updating pointers
- Stopping after first deletion
- Deleting wrong nodes by incorrect condition
Ready to prove it?
Pass the test and this pattern moves unknown
