Delete Node by Value in DSA Java - Time & Space Complexity
We want to understand how long it takes to delete a node by its value in a linked list.
How does the time needed change as the list gets bigger?
Analyze the time complexity of the following code snippet.
public void deleteNode(int value) {
Node current = head;
Node previous = null;
while (current != null) {
if (current.data == value) {
if (previous == null) {
head = current.next;
} else {
previous.next = current.next;
}
return;
}
previous = current;
current = current.next;
}
}
This code searches the linked list for the first node with the given value and removes it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing the linked list nodes one by one.
- How many times: Up to once for each node until the value is found or list ends.
As the list grows, the time to find the node grows roughly in direct proportion.
| 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 linearly as the list gets longer.
Time Complexity: O(n)
This means the time to delete a node grows in a straight line 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 this helps you explain how searching and deleting in linked lists works, a common task in coding problems.
"What if the list was doubly linked? How would the time complexity change when deleting 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 description
Removing the first node with the given value matches Remove the first node that contains the given value from the list, while other options describe different operations.Final Answer:
Remove the first node that contains the given value from the list -> Option DQuick Check:
Delete Node by Value = Remove matching node [OK]
- Confusing delete with add operation
- Thinking it finds position instead of deleting
- Assuming it reverses the list
head with value val?Solution
Step 1: Understand traversal to find node before target
To delete a node by value, we need to find the node before the target node to change its next pointer.Step 2: Check which loop finds the previous node
Node current = head; while(current.next != null && current.next.value != val) { current = current.next; } correctly loops whilecurrent.nextis not null and its value is not the target, socurrentstops at the node before the target.Final Answer:
Node current = head; while(current.next != null && current.next.value != val) { current = current.next; } -> Option CQuick Check:
Find previous node with current.next check = Node current = head; while(current.next != null && current.next.value != val) { current = current.next; } [OK]
- Checking current.value instead of current.next.value
- Not handling head node deletion separately
- Using while loop without null check causing errors
1 -> 2 -> 3 -> 4 -> null?Solution
Step 1: Locate node with value 3
The node with value 3 is the third node in the list: 1 -> 2 -> 3 -> 4 -> null.Step 2: Remove node with value 3 by linking previous to next
We link node with value 2 directly to node with value 4, skipping the node with value 3.Final Answer:
1 -> 2 -> 4 -> null -> Option AQuick Check:
Deleted node 3, list skips it = 1 -> 2 -> 4 -> null [OK]
- Not updating links, leaving deleted node in list
- Deleting wrong node by value
- Confusing node values and positions
Node current = head;
while(current.next != null && current.next.value != val) {
current = current.next;
}
if(current.next.value == val) {
current.next = current.next.next;
}What error can occur when deleting the node if the value
val is not present in the list?Solution
Step 1: Analyze loop exit condition
The loop stops whencurrent.next == nullorcurrent.next.value == val. Ifvalis not found,current.nextbecomes null.Step 2: Check the if condition usage
The code checkscurrent.next.value == valwithout verifying ifcurrent.nextis null, causing NullPointerException ifvalis missing.Final Answer:
NullPointerException at current.next.value -> Option AQuick Check:
Accessing value of null next node causes NullPointerException [OK]
- Assuming node always found, skipping null check
- Confusing runtime exceptions with compile errors
- Ignoring loop exit conditions
head with nodes 10 -> 20 -> 30 -> 40 -> 50 -> null, what will be the list after deleting the node with value 10 (the head node)?Solution
Step 1: Identify head node deletion case
The node to delete is the head node with value 10, so we must updateheadto point to the next node.Step 2: Update head pointer to next node
Sethead = head.next, which points to node with value 20, effectively removing the original head.Final Answer:
20 -> 30 -> 40 -> 50 -> null -> Option BQuick Check:
Deleting head means head moves to next node [OK]
- Not updating head pointer when deleting first node
- Removing wrong node by mistake
- Assuming list becomes empty after head deletion
Ready to prove it?
Pass the test and this pattern moves unknown
