Bird
Raised Fist0
DSA Javaprogramming~10 mins

Delete Node by Value in DSA Java - Execution Trace

Choose your learning style10 modes available
Concept Flow - Delete Node by Value
Start at head
↓
Check if head is null?
Yes→Stop: List empty
No↓
Check if head value == target?
Yes→Delete head, update head
No↓
Traverse list with prev and current
↓
Check current value == target?
Yes→Delete current by linking prev to current.next
No↓
Move prev and current forward
Back to Check current value↓
Reached end?
Yes→Stop: Value not found
↩Back to Check current value
Start from the head node, check if it matches the value to delete. If yes, remove head. Otherwise, traverse the list to find the node and remove it by linking previous node to next node.
Execution Sample
DSA Java
class Node {
  int val;
  Node next;
}

// Delete first node with value 3
Node deleteByValue(Node head, int val) {
  if (head == null) return head;
  if (head.val == val) {
    return head.next;
  }
  Node prev = head;
  Node current = head.next;
  while (current != null) {
    if (current.val == val) {
      prev.next = current.next;
      break;
    }
    prev = current;
    current = current.next;
  }
  return head;
}
Deletes the first node in the linked list that has the given value.
Execution Table
StepCurrent Node ValuePrev Node ValueConditionActionList State
11nullhead != null and head.val != 3Move to traversal1 -> 2 -> 3 -> 4 -> null
221current.val != 3Move prev to current, current to next1 -> 2 -> 3 -> 4 -> null
332current.val == 3Delete current by linking prev.next to current.next1 -> 2 -> 4 -> null
442Stop traversal after deletionEnd1 -> 2 -> 4 -> null
💡 Node with value 3 found and deleted, traversal stops.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
head1111
prevnull122
current1234
Key Moments - 3 Insights
Why do we check if the head node contains the value before traversal?
Because if the head node itself has the value, we must delete it by updating the head pointer. This is shown in execution_table row 1 where we check head.val before moving to traversal.
How do we delete a node that is not the head?
We link the previous node's next pointer to the current node's next, effectively skipping the current node. This is shown in execution_table row 3 where prev.next points to current.next.
What happens if the value is not found in the list?
The traversal reaches the end (current becomes null) without deletion, and the list remains unchanged. This is implied by the exit condition in the concept flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'prev' after step 2?
A1
B2
C3
Dnull
💡 Hint
Check the 'prev' column in variable_tracker after step 2.
At which step does the node get deleted?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Action' column in execution_table where deletion is described.
If the head node had value 3, what would happen at step 1?
ATraversal starts normally
BHead node is deleted and head updated
CNothing changes
DProgram crashes
💡 Hint
Refer to concept_flow where head node value check leads to deletion.
Concept Snapshot
Delete Node by Value in Linked List:
- Check if head is null or head.val matches value
- If head matches, update head to head.next
- Else, traverse with prev and current
- When current.val matches, link prev.next to current.next
- Stops after first deletion or end of list
- Maintains list integrity by skipping deleted node
Full Transcript
To delete a node by value in a linked list, start by checking if the list is empty. If not, check if the head node contains the value. If yes, delete the head by moving the head pointer to the next node. Otherwise, traverse the list with two pointers: prev and current. Move forward until you find the node with the target value. When found, delete it by linking prev.next to current.next, skipping the current node. Stop traversal after deletion. If the value is not found, the list remains unchanged.

Practice

(1/5)
1. What is the main purpose of the Delete Node by Value operation in a linked list?
easy
A. Reverse the linked list
B. Add a new node with the given value to the end of the list
C. Find the position of the node with the given value
D. Remove the first node that contains the given value from the list

Solution

  1. Step 1: Understand the operation goal

    The operation aims to remove a node that matches a specific value from the linked list.
  2. 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.
  3. Final Answer:

    Remove the first node that contains the given value from the list -> Option D
  4. Quick Check:

    Delete Node by Value = Remove matching node [OK]
Hint: Deleting by value means removing matching node, not adding [OK]
Common Mistakes:
  • Confusing delete with add operation
  • Thinking it finds position instead of deleting
  • Assuming it reverses the list
2. Which of the following is the correct Java syntax to start deleting a node by value from a singly linked list named head with value val?
easy
A. Node current = head; while(current != null && current.value != val) { current = current.next; }
B. Node current = head; while(current.value != val) { current = current.next; }
C. Node current = head; while(current.next != null && current.next.value != val) { current = current.next; }
D. Node current = head; if(current.value == val) { head = head.next; }

Solution

  1. 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.
  2. 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 while current.next is not null and its value is not the target, so current stops at the node before the target.
  3. Final Answer:

    Node current = head; while(current.next != null && current.next.value != val) { current = current.next; } -> Option C
  4. Quick Check:

    Find previous node with current.next check = Node current = head; while(current.next != null && current.next.value != val) { current = current.next; } [OK]
Hint: Loop to node before target uses current.next, not current [OK]
Common Mistakes:
  • Checking current.value instead of current.next.value
  • Not handling head node deletion separately
  • Using while loop without null check causing errors
3. What will be the output linked list after deleting the node with value 3 from the list: 1 -> 2 -> 3 -> 4 -> null?
medium
A. 1 -> 2 -> 4 -> null
B. 1 -> 3 -> 4 -> null
C. 2 -> 3 -> 4 -> null
D. 1 -> 2 -> 3 -> 4 -> null

Solution

  1. Step 1: Locate node with value 3

    The node with value 3 is the third node in the list: 1 -> 2 -> 3 -> 4 -> null.
  2. 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.
  3. Final Answer:

    1 -> 2 -> 4 -> null -> Option A
  4. Quick Check:

    Deleted node 3, list skips it = 1 -> 2 -> 4 -> null [OK]
Hint: Skip node with value by linking previous to next [OK]
Common Mistakes:
  • Not updating links, leaving deleted node in list
  • Deleting wrong node by value
  • Confusing node values and positions
4. Consider this Java code snippet to delete a node by value from a singly linked list:
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?
medium
A. NullPointerException at current.next.value
B. IndexOutOfBoundsException at current.next.next
C. Compilation error due to missing semicolon
D. No error, deletion works fine

Solution

  1. Step 1: Analyze loop exit condition

    The loop stops when current.next == null or current.next.value == val. If val is not found, current.next becomes null.
  2. Step 2: Check the if condition usage

    The code checks current.next.value == val without verifying if current.next is null, causing NullPointerException if val is missing.
  3. Final Answer:

    NullPointerException at current.next.value -> Option A
  4. Quick Check:

    Accessing value of null next node causes NullPointerException [OK]
Hint: Always check for null before accessing next.value [OK]
Common Mistakes:
  • Assuming node always found, skipping null check
  • Confusing runtime exceptions with compile errors
  • Ignoring loop exit conditions
5. Given a singly linked list head with nodes 10 -> 20 -> 30 -> 40 -> 50 -> null, what will be the list after deleting the node with value 10 (the head node)?
hard
A. null
B. 20 -> 30 -> 40 -> 50 -> null
C. 30 -> 40 -> 50 -> null
D. 10 -> 20 -> 30 -> 40 -> 50 -> null

Solution

  1. Step 1: Identify head node deletion case

    The node to delete is the head node with value 10, so we must update head to point to the next node.
  2. Step 2: Update head pointer to next node

    Set head = head.next, which points to node with value 20, effectively removing the original head.
  3. Final Answer:

    20 -> 30 -> 40 -> 50 -> null -> Option B
  4. Quick Check:

    Deleting head means head moves to next node [OK]
Hint: Deleting head means move head pointer to next node [OK]
Common Mistakes:
  • Not updating head pointer when deleting first node
  • Removing wrong node by mistake
  • Assuming list becomes empty after head deletion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard