Delete by Value in Doubly Linked List in DSA Javascript - Time & Space Complexity
We want to understand how long it takes to remove a value from a doubly linked list.
How does the time needed change when the list gets bigger?
Analyze the time complexity of the following code snippet.
function deleteByValue(head, value) {
let current = head;
while (current !== null) {
if (current.data === value) {
if (current.prev) current.prev.next = current.next;
if (current.next) current.next.prev = current.prev;
if (current === head) head = current.next;
break;
}
current = current.next;
}
return head;
}
This code searches the list for the first node with the given value and removes it by updating pointers.
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 a node by value is always very fast, like O(1)."
[OK] Correct: You must first find the node by checking each one, which can take time proportional to the list size.
Understanding this helps you explain how linked lists work and how searching affects performance in real tasks.
"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 value matches the target.Step 2: Remove the node and update links
Adjust the previous node's next pointer and the next node's previous pointer to bypass the deleted node.Final Answer:
The node with the matching value is removed and links are updated -> Option AQuick Check:
Delete by value means remove matching node [OK]
- Deleting all nodes instead of one
- Not updating previous or next pointers
- Deleting wrong node when multiple have same value
Solution
Step 1: Understand JavaScript equality operators
Use === for strict equality to avoid type coercion and ensure exact match.Step 2: Identify correct syntax for comparison
if (current.value === target) correctly compares values and types.Final Answer:
if (current.value === target) {} -> Option DQuick Check:
Use strict equality (===) for value comparison [OK]
- Using single = which assigns instead of compares
- Using == which allows type coercion
- Using != which checks inequality
Solution
Step 1: Identify node to delete
The node with value 30 is located 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, skipping 30.Final Answer:
10 <-> 20 <-> 40 -> Option BQuick Check:
Deleted 30, linked 20 and 40 directly [OK]
- Not updating previous pointer of next node
- Leaving deleted node linked in list
- Deleting wrong node
function deleteByValue(head, value) {
let current = head;
while (current !== null) {
if (current.value === value) {
current.prev.next = current.next;
current.next.prev = current.prev;
break;
}
current = current.next;
}
return head;
}Solution
Step 1: Analyze deletion of head node
If the node to delete is the head, current.prev is null, so current.prev.next causes error.Step 2: Identify missing head update
Code should update head to current.next if deleting head node to avoid broken list.Final Answer:
It does not handle deleting the head node correctly -> Option CQuick Check:
Deleting head needs special handling [OK]
- Not checking if current.prev is null
- Not updating head when deleting first node
- Assuming all nodes have prev and next
Solution
Step 1: Delete head node with value 5
Update head to 10 and set 10.prev to null.Step 2: Delete tail node with value 25
Update 20.next to null, removing 25.Final Answer:
10 <-> 15 <-> 20 -> Option AQuick Check:
Removed 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
