Delete Node by Value in DSA Typescript - Time & Space Complexity
When we delete a node by value from 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 the node change with list size?
Analyze the time complexity of the following code snippet.
function deleteNode(head: ListNode | null, value: number): ListNode | null {
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 the linked list for the first node with the given value and removes it by changing pointers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves through the list nodes one by one.
- How many times: Up to once for each node until the value is found or the list ends.
As the list gets longer, the search may take more steps because it checks nodes one by one.
| 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 a node grows linearly with the number of nodes in the list.
[X] Wrong: "Deleting a node by value always takes constant time because we just change pointers."
[OK] Correct: We must first find the node by checking nodes one by one, which takes 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 doubly linked? How would the time complexity change when deleting a node by value?"
Practice
Delete Node by Value operation in a linked list?Solution
Step 1: Understand the operation goal
The operation aims to remove a node that matches a specific value from the linked list.Step 2: Identify the correct action
Removing the first node with the given value means deleting that node and linking its previous node to the next node.Final Answer:
Remove the first node that contains the given value -> Option CQuick Check:
Delete node by value = Remove first matching node [OK]
- Confusing deletion with insertion
- Trying to delete all nodes with the value instead of first
- Thinking it finds position only
- Assuming it reverses the list
head?Solution
Step 1: Check if head node has value 5
If the first node's value is 5, we remove it by moving head to the next node.Step 2: Verify code correctness
if (head.value === 5) head = head.next; correctly updates head to head.next if head.value is 5, effectively deleting the first node.Final Answer:
if (head.value === 5) head = head.next; -> Option AQuick Check:
Delete head node by reassigning head [OK]
- Using incorrect conditional expressions
- Assigning head.next incorrectly
- Not handling head node deletion
- Overwriting head without checks
let current = head;
while (current.next !== null) {
if (current.next.value === 5) {
current.next = current.next.next;
break;
}
current = current.next;
}Solution
Step 1: Traverse list to find node before value 5
Loop checks nodes until current.next.value is 5, which is true when current is node with value 3.Step 2: Delete node with value 5 by skipping it
Set current.next to current.next.next, linking node 3 directly to node 7, removing node 5.Final Answer:
1 -> 3 -> 7 -> null -> Option BQuick Check:
Skip node with value 5 = 1->3->7 [OK]
- Not breaking loop after deletion
- Deleting wrong node
- Not updating pointers correctly
- Confusing current and current.next
function deleteNode(head: Node | null, value: number): Node | null {
let current = head;
while (current !== null) {
if (current.next?.value === value) {
current.next = current.next.next;
}
current = current.next!;
}
return head;
}
What is the main problem with this code?Solution
Step 1: Analyze the loop termination
The while (current !== null) continues to the last node where current.next is null. The if uses ?. safely, but then executes current = current.next!;, and ! non-null assertion on null causes runtime error.Step 2: Confirm the bug triggers always
For any non-empty list, it crashes at the last node regardless of value, preventing correct execution.Final Answer:
It may cause a runtime error when current.next is null -> Option AQuick Check:
current.next! on null = runtime error [OK]
- Using non-null assertion (!) on potentially null next
- Assuming current.next always exists
- Not breaking after deletion
- Confusing current and current.next updates
Solution
Step 1: Delete first node with value 4
The first 4 is the second node, so remove it by linking node 2 to node 6.Step 2: Insert new node with value 10 after node 2
Create new node 10 and link it after node 2, then link new node to node 6.Final Answer:
Delete first 4, then insert 10 after node 2 -> Option DQuick Check:
Delete 4, insert 10 after previous node [OK]
- Inserting new node at wrong position
- Deleting all nodes with value 4
- Not updating links correctly
- Inserting before deletion
Ready to prove it?
Pass the test and this pattern moves unknown
