Bird
Raised Fist0
DSA Javaprogramming~20 mins

Delete Node by Value in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Delete Node Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after deleting a node by value in a singly linked list?
Consider the following Java code that deletes a node with a given value from a singly linked list. What will be the printed list after deleting the node with value 3?
DSA Java
class Node {
    int data;
    Node next;
    Node(int data) { this.data = data; this.next = null; }
}

class LinkedList {
    Node head;

    void append(int data) {
        if (head == null) {
            head = new Node(data);
            return;
        }
        Node current = head;
        while (current.next != null) {
            current = current.next;
        }
        current.next = new Node(data);
    }

    void deleteByValue(int value) {
        if (head == null) return;
        if (head.data == value) {
            head = head.next;
            return;
        }
        Node current = head;
        while (current.next != null && current.next.data != value) {
            current = current.next;
        }
        if (current.next != null) {
            current.next = current.next.next;
        }
    }

    void printList() {
        Node current = head;
        while (current != null) {
            System.out.print(current.data + " -> ");
            current = current.next;
        }
        System.out.println("null");
    }
}

public class Main {
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.append(1);
        list.append(2);
        list.append(3);
        list.append(4);
        list.deleteByValue(3);
        list.printList();
    }
}
A2 -> 3 -> 4 -> null
B1 -> 2 -> 3 -> 4 -> null
C1 -> 2 -> 4 -> null
D1 -> 2 -> null
Attempts:
2 left
💡 Hint
Think about how the deleteByValue method skips the node with the matching value.
❓ Predict Output
intermediate
2:00remaining
What happens if we delete the head node by value?
Given the same linked list code, what will be the output after deleting the node with value 1 (the head node)?
DSA Java
LinkedList list = new LinkedList();
list.append(1);
list.append(2);
list.append(3);
list.deleteByValue(1);
list.printList();
A1 -> 2 -> 3 -> null
B2 -> 3 -> null
C3 -> null
Dnull
Attempts:
2 left
💡 Hint
Deleting the head means the head pointer moves to the next node.
🔧 Debug
advanced
2:00remaining
Why does this deleteByValue method fail to delete the last node?
Examine the following deleteByValue method. It fails to delete the last node if it matches the value. What is the reason?
DSA Java
void deleteByValue(int value) {
    if (head == null) return;
    if (head.data == value) {
        head = head.next;
        return;
    }
    Node current = head;
    while (current.next != null && current.next.data != value) {
        current = current.next;
    }
    if (current.next != null) {
        current.next = current.next.next;
    }
}
AThe method incorrectly updates the head pointer.
BThe method does not handle empty lists.
CThe head node is not checked for deletion.
DThe loop condition stops before checking the last node's value.
Attempts:
2 left
💡 Hint
Look carefully at the loop condition and what it allows current.next to be.
❓ Predict Output
advanced
2:00remaining
What is the output after deleting a non-existent value?
What will be the output of the list after trying to delete a node with value 5, which does not exist in the list?
DSA Java
LinkedList list = new LinkedList();
list.append(1);
list.append(2);
list.append(3);
list.deleteByValue(5);
list.printList();
A1 -> 2 -> 3 -> null
B1 -> 2 -> null
C2 -> 3 -> null
Dnull
Attempts:
2 left
💡 Hint
Deleting a value not in the list should not change the list.
🧠 Conceptual
expert
2:00remaining
What is the time complexity of deleting a node by value in a singly linked list?
Consider a singly linked list with n nodes. What is the time complexity of deleting a node by value using the standard approach shown in the code?
AO(n) because we may need to traverse the entire list to find the node.
BO(1) because deletion is done by adjusting pointers.
CO(log n) because the list is sorted.
DO(n^2) because of nested loops.
Attempts:
2 left
💡 Hint
Think about how many nodes you might need to check before finding the value.

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