Delete Node by Value in DSA Javascript - Time & Space Complexity
When we delete a node by value in a linked list, we want to know how long it takes as the list grows.
We ask: How does the time to find and remove a node change with the list size?
Analyze the time complexity of the following code snippet.
function deleteNode(head, value) {
if (!head) return null;
if (head.val === value) return head.next;
let current = head;
while (current.next && current.next.val !== value) {
current = current.next;
}
if (current.next) {
current.next = current.next.next;
}
return head;
}
This code searches for a node with a 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 nodes one by one.
- How many times: Up to once for each node until the value is found or list ends.
As the list gets longer, the search may take more steps.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 steps |
| 100 | Up to 100 steps |
| 1000 | Up to 1000 steps |
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 a node grows linearly with the number of nodes in the list.
[X] Wrong: "Deleting a node by value always takes the same time no matter the list size."
[OK] Correct: Because the code may need to check many nodes before finding the value or reaching the end.
Understanding how searching through a list affects time helps you explain your code clearly and shows you know how data size impacts performance.
"What if the list was doubly linked? How would the time complexity change when deleting a node by value?"
Practice
deleteNodeByValue function in a linked list?Solution
Step 1: Understand the function's goal
The function aims to remove a node that matches a specific value from the linked list.Step 2: Identify the action performed
Deleting a node by value means finding the first node with that value and removing it from the list.Final Answer:
To remove the first node that contains the given value from the list -> Option DQuick Check:
Delete node by value = Remove node with value [OK]
- Confusing deletion with insertion
- Thinking it finds position only
- Assuming it reverses the list
Solution
Step 1: Understand comparison syntax in JavaScript
Use triple equals (===) to compare values strictly without type coercion.Step 2: Identify correct condition for head node value check
Check ifhead.valueequals the targetvalueusing===.Final Answer:
if (head.value === value) { /* delete head */ } -> Option AQuick Check:
Use === for value comparison [OK]
- Using single = which assigns instead of compares
- Using == which can cause unexpected type coercion
- Comparing head node directly to value without .value
deleteNodeByValue(head, 30), what will be the new list?Solution
Step 1: Locate the node with value 30
Traverse the list: 10, 20, then 30 is found as the third node.Step 2: Remove the node with value 30
Adjust the previous node's next pointer (20's next) to point to 40, skipping 30.Final Answer:
10 -> 20 -> 40 -> null -> Option CQuick Check:
Deleted 30 node, list skips it [OK]
- Not updating previous node's next pointer
- Deleting wrong node or none
- Confusing node values or order
function deleteNodeByValue(head, value) {
let current = head;
while (current.next !== null) {
if (current.next.value === value) {
current.next = current.next.next;
return head;
}
current = current.next;
}
return head;
}Solution
Step 1: Analyze head node deletion case
The code starts checking fromcurrent.next, so it never checks ifheaditself has the value.Step 2: Identify missing head deletion handling
If the head node contains the value, it won't be deleted, causing incorrect behavior.Final Answer:
It does not handle deleting the head node if it matches the value -> Option BQuick Check:
Head node deletion missing = Bug [OK]
- Assuming loop covers head node
- Ignoring empty list case
- Thinking it deletes all matching nodes
Solution
Step 1: Identify the first node with value 10
Traverse from head: 5 (no), then 10 (yes) at second node.Step 2: Delete the first 10 node
Link 5's next pointer to 15, skipping the first 10 node.Final Answer:
5 -> 15 -> 10 -> 20 -> null -> Option AQuick Check:
Only first 10 removed, rest remain [OK]
- Deleting all nodes with value 10
- Deleting wrong 10 node
- Breaking list links incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
