Delete by Value in Doubly Linked List in DSA Go - Time & Space Complexity
When deleting a node by value in 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.
func deleteByValue(head *Node, val int) *Node {
current := head
for current != nil {
if current.data == val {
if current.prev != nil {
current.prev.next = current.next
} else {
head = current.next
}
if current.next != nil {
current.next.prev = current.prev
}
break
}
current = current.next
}
return head
}
This code searches for the first node with the given value and removes it from the doubly linked list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing the list node by node 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 search may take longer because it might check more nodes.
| 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 operations grows roughly in direct proportion to the list size.
Time Complexity: O(n)
This means the time to delete by value grows linearly with the number of nodes in the list.
[X] Wrong: "Deleting by value is always fast because we just remove the node."
[OK] Correct: We must first find the node by checking each 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 interviews.
"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 doubly linked list from head to tail to find the node whose data matches the value to delete.Step 2: Update the 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, effectively removing it from the list.Final Answer:
The node with the matching value is removed and links are updated -> Option AQuick Check:
Delete by value means remove matching node and fix links [OK]
- Not updating previous node's next pointer
- Not updating next node's previous pointer
- Deleting wrong node when multiple have same value
Solution
Step 1: Check node data against value
The condition current.data == value correctly identifies the node to delete.Step 2: Update links correctly
Setting current.prev.next = current.next and current.next.prev = current.prev properly removes the node by linking its neighbors.Final Answer:
if current.data == value { current.prev.next = current.next; current.next.prev = current.prev } -> Option DQuick Check:
Correct link updates remove node [OK]
- Swapping prev and next pointers incorrectly
- Setting current to nil without fixing links
- Accessing next or prev without nil checks causing panic
Solution
Step 1: Identify node with value 3
Traverse the list to find the node containing 3, which is between nodes 2 and 4.Step 2: Remove node 3 and update links
Set node 2's next pointer to node 4 and node 4's previous pointer to node 2, skipping node 3.Final Answer:
1 <-> 2 <-> 4 <-> 5 -> Option BQuick Check:
Node 3 removed, neighbors linked [OK]
- Leaving node 3 linked causing broken list
- Deleting wrong node by value
- Not updating both prev and next pointers
func deleteByValue(head *Node, value int) *Node {
current := head
for current != nil {
if current.data == value {
if current.prev != nil {
current.prev.next = current.next
} else {
head = current.next
}
if current.next != nil {
current.next.prev = current.prev
}
break
}
current = current.next
}
return head
}
What is the bug in this code?Solution
Step 1: Analyze loop and break behavior
The loop breaks immediately after deleting the first node with matching value, so only one node is deleted even if multiple nodes have the same value.Step 2: Understand implications for multiple matches
If the list has multiple nodes with the same value, only the first is removed, which may not be the intended behavior if all matches should be deleted.Final Answer:
It breaks after deleting only the first matching node -> Option CQuick Check:
Break stops after first delete [OK]
- Ignoring multiple nodes with same value
- Not updating head when first node deleted
- Not checking nil before pointer access
Solution
Step 1: Traverse entire list without breaking
To delete all nodes with value 20, you must check every node and not stop after the first deletion.Step 2: Delete matching nodes and update links carefully
For each node with value 20, update the previous and next pointers to remove it, then continue traversal to find others.Final Answer:
Traverse the list and delete nodes with value 20, continuing without breaking the loop -> Option AQuick Check:
Delete all matches by full traversal [OK]
- Breaking loop after first deletion
- Not updating links properly causing broken list
- Only deleting head or tail nodes
Ready to prove it?
Pass the test and this pattern moves unknown
