Delete Node by Value in DSA C# - Time & Space Complexity
We want to understand how long it takes to remove a node by its value from a linked list.
How does the time needed change when 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: Looping through the linked list nodes one by one.
- 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 node 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 a node grows in a straight line as the list gets longer.
[X] Wrong: "Deleting a node by value always takes the same time no matter the list size."
[OK] Correct: Because you may need to look through many nodes before finding the value, so time grows with list length.
Understanding this helps you explain how searching and deleting in linked lists works, a key skill for many coding tasks.
"What if the list was doubly linked? How would the time complexity change?"
Practice
Delete Node by Value do in a linked list?Solution
Step 1: Understand the operation
Deleting a node by value means searching for the first node that matches the value and removing it from the linked list.Step 2: Compare options
Only Removes the first node that contains the given value from the list describes removing the first node with the given value. Others describe adding, finding without removing, or reversing, which are different operations.Final Answer:
Removes the first node that contains the given value from the list -> Option CQuick Check:
Delete Node by Value = Remove first matching node [OK]
- Confusing deletion by value with adding a node
- Thinking it removes all nodes with that value
- Assuming it reverses the list
Solution
Step 1: Check method signature conventions in C#
In C#, methods usually have an access modifier like 'public' and a return type. Deleting a node usually returns void.Step 2: Analyze options
public void DeleteNode(int value) { /* code */ } uses 'public void DeleteNode(int value)', which is correct. void DeleteNodeByValue(int value) { /* code */ } misses access modifier. public int DeleteNode(int value) { /* code */ } returns int which is uncommon here. DeleteNode(int value) => { /* code */ } uses invalid lambda syntax for method.Final Answer:
public void DeleteNode(int value) { /* code */ } -> Option BQuick Check:
Correct method signature = public void DeleteNode(int value) { /* code */ } [OK]
- Omitting access modifier
- Using wrong return type
- Incorrect method syntax
Solution
Step 1: Identify the node to delete
The node with value 30 is the third node in the list.Step 2: Remove the node and link previous to next
After deleting 30, the node with value 20 points directly to 40, so the list becomes 10 -> 20 -> 40 -> null.Final Answer:
10 -> 20 -> 40 -> null -> Option AQuick Check:
Deleted 30, list skips it [OK]
- Not updating links after deletion
- Deleting wrong node
- Leaving deleted node in list
public void DeleteNode(int value) {
Node current = head;
Node previous = null;
while (current != null && current.data != value) {
previous = current;
current = current.next;
}
if (current == null) return;
previous.next = current.next;
}
What error will occur when deleting the head node?Solution
Step 1: Analyze deletion of head node
If the node to delete is head, previous is still null because loop never runs.Step 2: Check assignment previous.next = current.next
Since previous is null, trying to access previous.next causes NullReferenceException.Final Answer:
NullReferenceException because previous is null when deleting head -> Option DQuick Check:
Deleting head needs special case to avoid null error [OK]
- Not checking if node to delete is head
- Assuming previous is never null
- Ignoring null pointer exceptions
Solution
Step 1: Delete head node with value 5
Removing head means head moves to next node, so list becomes 10 -> 15 -> 20 -> 25 -> null.Step 2: Delete node with value 20
Find node 20 and remove it by linking 15 to 25, resulting in 10 -> 15 -> 25 -> null.Final Answer:
10 -> 15 -> 25 -> null -> Option AQuick Check:
Deleted head and middle node correctly [OK]
- Not updating head after deleting first node
- Not linking nodes correctly after deletion
- Deleting wrong nodes
Ready to prove it?
Pass the test and this pattern moves unknown
