Delete by Value in Doubly Linked List in DSA Java - Time & Space Complexity
When deleting a node by value in a doubly linked list, we want to know how the time needed changes as the list grows.
We ask: How long does it take to find and remove the node with the given value?
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 pointers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing the list nodes one by one to find the matching 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 a straight line.
| 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 operations increase directly with the number of nodes; doubling nodes roughly doubles work.
Time Complexity: O(n)
This means the time to delete by value grows linearly with the list size because we may need to check each node once.
[X] Wrong: "Deleting by value is always fast because we just remove the node."
[OK] Correct: Before removing, we must 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 speed in linked lists, a common topic in coding discussions.
"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 AQuick Check:
Deleting by value removes matching node [OK]
- Not updating both previous and next pointers
- Deleting all nodes instead of one
- Ignoring the case when node is head or tail
Solution
Step 1: Check method return type and parameters
The method should accept an integer value to delete and return nothing, so void return type is correct.Step 2: Verify method signature syntax
Java requires return type before method name and parameter types inside parentheses.Final Answer:
void deleteByValue(int value) { /* code */ } -> Option BQuick Check:
Method with void return and int parameter is correct [OK]
- Omitting return type
- Missing parameter type
- Using wrong return type for delete method
Solution
Step 1: Identify node with value 30
Traverse the list and find the node containing 30 between 20 and 40.Step 2: Remove node 30 and update links
Set 20's next pointer to 40 and 40's previous pointer to 20, removing 30 from the chain.Final Answer:
10 <-> 20 <-> 40 <-> null -> Option AQuick Check:
Node 30 removed, links updated correctly [OK]
- Not updating previous pointer of next node
- Leaving deleted node linked
- Deleting wrong node
void deleteByValue(int value) {
Node current = head;
while (current != null && current.data != value) {
current = current.next;
}
if (current == null) return;
current.prev.next = current.next;
current.next.prev = current.prev;
}Solution
Step 1: Analyze deletion at head node
If current is head, current.prev is null, so current.prev.next causes NullPointerException.Step 2: Identify missing condition for head deletion
Code must check if current.prev is null and update head pointer accordingly.Final Answer:
It does not handle deletion when current is head -> Option DQuick Check:
Head deletion needs special handling [OK]
- Not handling head or tail nodes separately
- Accessing null pointers without checks
- Assuming node always has previous and next
Solution
Step 1: Delete node with value 5 (head)
Since 5 is head, update head to 10 and set 10's prev to null.Step 2: Delete node with value 25 (tail)
Since 25 is tail, update 20's next to null, removing 25.Final Answer:
10 <-> 15 <-> 20 <-> null -> Option CQuick Check:
Head and tail deletions handled correctly [OK]
- Not updating head when deleting first node
- Not updating tail when deleting last node
- Breaking links causing list corruption
Ready to prove it?
Pass the test and this pattern moves unknown
