Bird
Raised Fist0
DSA Javaprogramming

Delete Node by Value in DSA Java

Choose your learning style10 modes available
Mental Model
To remove a node with a specific value, find it by moving through the list, then change the previous node to skip it.
Analogy: Imagine a chain of paper clips linked together. To remove one clip, you unhook the one before it and link it directly to the one after, so the chain stays connected without the removed clip.
head -> 1 -> 2 -> 3 -> 4 -> null
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> 4 -> null, delete value 3
Goal: Remove the node containing value 3 and keep the list connected
Step 1: start at head node with value 1
head -> [curr->1] -> 2 -> 3 -> 4 -> null
Why: We begin searching from the start to find the node to delete
Step 2: move curr to node with value 2, keep track of prev as node 1
head -> 1 -> [curr->2] -> 3 -> 4 -> null
Why: We check each node to find the target value
Step 3: move curr to node with value 3, prev is node 2
head -> 1 -> 2 -> [curr->3] -> 4 -> null
Why: Found the node with value 3 to delete
Step 4: change prev.next to curr.next, skipping node 3
head -> 1 -> 2 -> 4 -> null
Why: By linking node 2 directly to node 4, node 3 is removed
Result:
head -> 1 -> 2 -> 4 -> null
Annotated Code
DSA Java
class Node {
    int val;
    Node next;
    Node(int val) { this.val = val; this.next = null; }
}

public class DeleteNodeByValue {
    public static Node deleteNode(Node head, int value) {
        if (head == null) return null; // empty list
        if (head.val == value) return head.next; // delete head node

        Node prev = head;
        Node curr = head.next;

        while (curr != null) {
            if (curr.val == value) {
                prev.next = curr.next; // skip the node to delete
                break;
            }
            prev = curr; // move prev forward
            curr = curr.next; // move curr forward
        }
        return head;
    }

    public static void printList(Node head) {
        Node curr = head;
        while (curr != null) {
            System.out.print(curr.val);
            if (curr.next != null) System.out.print(" -> ");
            curr = curr.next;
        }
        System.out.println(" -> null");
    }

    public static void main(String[] args) {
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);
        head.next.next.next = new Node(4);

        System.out.print("Original list: ");
        printList(head);

        head = deleteNode(head, 3);

        System.out.print("After deleting 3: ");
        printList(head);
    }
}
if (head == null) return null; // empty list
handle empty list edge case
if (head.val == value) return head.next; // delete head node
if head node matches value, remove it by returning next node
while (curr != null) {
traverse list to find node with target value
if (curr.val == value) {
found node to delete
prev.next = curr.next; // skip the node to delete
unlink node by skipping it in the chain
prev = curr; // move prev forward
advance prev pointer to keep track
curr = curr.next; // move curr forward
advance curr pointer to continue search
OutputSuccess
Original list: 1 -> 2 -> 3 -> 4 -> null After deleting 3: 1 -> 2 -> 4 -> null
Complexity Analysis
Time: O(n) because we may need to check each node once to find the value
Space: O(1) because we only use a few pointers, no extra storage
vs Alternative: Compared to rebuilding the list without the value, this approach is faster and uses less memory by changing pointers directly
Edge Cases
empty list
returns null immediately, no deletion
DSA Java
if (head == null) return null; // empty list
value is at head node
head is removed by returning next node
DSA Java
if (head.val == value) return head.next; // delete head node
value not found in list
list remains unchanged after traversal
DSA Java
while (curr != null) { ... }
When to Use This Pattern
When you need to remove a specific value from a linked list, look for the delete-by-value pattern that adjusts pointers to skip the target node.
Common Mistakes
Mistake: Not handling deletion of the head node separately
Fix: Add a check to remove the head if it matches the value before traversing
Mistake: Not updating the previous node's next pointer correctly
Fix: Ensure prev.next is set to curr.next to unlink the target node
Summary
Deletes the first node with a given value from a linked list by adjusting pointers.
Use when you want to remove a specific value from a singly linked list.
The key is to find the node and link its previous node directly to its next node, skipping it.

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