Delete by Value in Doubly Linked List in DSA Typescript - Time & Space Complexity
When deleting a value from a doubly 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 value change with list size?
Analyze the time complexity of the following code snippet.
function deleteByValue(head: Node | null, value: number): Node | null {
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 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 a straight line.
| 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 directly 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: Finding the node takes time because we may need to check many nodes before finding the value.
Understanding this helps you explain how linked lists work and how operations scale, a key skill in many coding interviews.
"What if the list was sorted? How would that change the time complexity of deleting by value?"
Practice
Solution
Step 1: Identify 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 exclude the found node.Final Answer:
The node with the matching value is removed and links are updated -> Option BQuick Check:
Deleting by value removes matching node [OK]
- Not updating previous node's next pointer
- Not updating next node's previous pointer
- Deleting all nodes instead of one
Solution
Step 1: Check method declaration syntax in TypeScript class
Methods inside classes use the syntax methodName(params): returnType { body }.Step 2: Identify correct syntax for deleteNode method
deleteNode(value: number): void { /* code */ } uses correct TypeScript method syntax with type annotation and void return.Final Answer:
deleteNode(value: number): void { /* code */ } -> Option CQuick Check:
TypeScript method syntax = deleteNode(value: number): void { /* code */ } [OK]
- Using arrow function syntax inside class method
- Missing type annotations
- Using invalid keywords like 'delete node'
Solution
Step 1: Locate node with value 30
Traverse the list: 10 -> 20 -> 30 found at third node.Step 2: Remove node 30 and link 20 to 40
Update 20's next pointer to 40 and 40's previous pointer to 20.Final Answer:
10 <-> 20 <-> 40 -> Option AQuick Check:
Deleted 30, list links updated [OK]
- Not updating previous node's next pointer
- Not updating next node's previous pointer
- Deleting wrong node
deleteNode(value: number): void {
let current = this.head;
while (current !== null) {
if (current.value === value) {
current.prev.next = current.next;
current.next.prev = current.prev;
break;
}
current = current.next;
}
}Solution
Step 1: Check handling of head node deletion
If current is head, current.prev is null, so current.prev.next causes error.Step 2: Identify missing head update
Function does not update this.head when deleting the first node, causing broken list.Final Answer:
It does not handle deleting the head node correctly -> Option AQuick Check:
Head deletion needs special handling [OK]
- Not checking if current.prev is null
- Not updating head pointer when deleting first node
- Assuming all nodes have prev and next
Solution
Step 1: Delete node with value 5 (head node)
Update head to 10, set 10.prev to null.Step 2: Delete node with value 25 (tail node)
Update 20.next to null, removing 25.Final Answer:
10 <-> 15 <-> 20 -> Option DQuick Check:
Deleted head and tail nodes correctly [OK]
- Not updating head when deleting first node
- Not updating tail when deleting last node
- Breaking links between middle nodes
Ready to prove it?
Pass the test and this pattern moves unknown
