Bird
Raised Fist0
DSA Javaprogramming~20 mins

Delete by Value in Doubly Linked List in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Doubly Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after deleting a value from doubly linked list
What is the printed state of the doubly linked list after deleting the value 3?
DSA Java
class Node {
    int data;
    Node prev, next;
    Node(int d) { data = d; prev = next = null; }
}

class DoublyLinkedList {
    Node head;

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

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

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

public class Main {
    public static void main(String[] args) {
        DoublyLinkedList dll = new DoublyLinkedList();
        dll.append(1);
        dll.append(2);
        dll.append(3);
        dll.append(4);
        dll.append(5);
        dll.deleteByValue(3);
        dll.printList();
    }
}
A2 -> 3 -> 4 -> 5 -> null
B1 -> 2 -> 3 -> 4 -> 5 -> null
C1 -> 2 -> 4 -> null
D1 -> 2 -> 4 -> 5 -> null
Attempts:
2 left
💡 Hint
Focus on how the deleteByValue method updates the links when the node with value 3 is found.
🧠 Conceptual
intermediate
1:30remaining
Understanding node deletion in doubly linked list
When deleting a node by value in a doubly linked list, which pointers must be updated to maintain list integrity?
ANo pointers need to be updated
BOnly the next node's prev pointer
CBoth the previous node's next pointer and the next node's prev pointer
DOnly the previous node's next pointer
Attempts:
2 left
💡 Hint
Think about how nodes are connected in both directions.
🔧 Debug
advanced
2:00remaining
Identify the error in deleteByValue method
What error will occur when running this deleteByValue method if the value to delete is not found in the list?
DSA Java
void deleteByValue(int value) {
    Node current = head;
    while (current != null) {
        if (current.data == value) {
            if (current.prev != null) current.prev.next = current.next;
            else head = current.next;
            if (current.next != null) current.next.prev = current.prev;
            break;
        }
        current = current.next;
    }
    System.out.println(current.data);
}
ANo error, prints the value
BNullPointerException
CArrayIndexOutOfBoundsException
DCompilation error
Attempts:
2 left
💡 Hint
Consider what happens if the value is not found and current becomes null.
❓ Predict Output
advanced
2:00remaining
Output after deleting head node by value
What is the printed state of the doubly linked list after deleting the value 1 (head node)?
DSA Java
class Node {
    int data;
    Node prev, next;
    Node(int d) { data = d; prev = next = null; }
}

class DoublyLinkedList {
    Node head;

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

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

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

public class Main {
    public static void main(String[] args) {
        DoublyLinkedList dll = new DoublyLinkedList();
        dll.append(1);
        dll.append(2);
        dll.append(3);
        dll.deleteByValue(1);
        dll.printList();
    }
}
A2 -> 3 -> null
B1 -> 2 -> 3 -> null
C3 -> null
Dnull
Attempts:
2 left
💡 Hint
Deleting the head node updates the head pointer to the next node.
🧠 Conceptual
expert
2:30remaining
Why is doubly linked list deletion by value more efficient than singly linked list?
Which reason best explains why deleting a node by value in a doubly linked list can be more efficient than in a singly linked list?
ABecause doubly linked list nodes have pointers to both previous and next nodes, allowing direct access to update links without traversal to find the previous node.
BBecause doubly linked lists use less memory, making deletion faster.
CBecause singly linked lists do not allow deletion by value.
DBecause doubly linked lists store values in sorted order by default.
Attempts:
2 left
💡 Hint
Think about how the previous node is found in singly vs doubly linked lists.

Practice

(1/5)
1. What happens when you delete a node by value in a doubly linked list?
easy
A. The node with the matching value is removed and links are updated
B. All nodes are deleted regardless of value
C. Only the first node is deleted no matter the value
D. The list is reversed instead of deleting

Solution

  1. Step 1: Locate the node with the given value

    Traverse the list from head to tail to find the node whose data matches the value to delete.
  2. Step 2: Update links to remove the node

    Adjust the previous node's next pointer and the next node's previous pointer to bypass the node to be deleted.
  3. Final Answer:

    The node with the matching value is removed and links are updated -> Option A
  4. Quick Check:

    Deleting by value removes matching node [OK]
Hint: Find node by value, then update previous and next links [OK]
Common Mistakes:
  • Not updating both previous and next pointers
  • Deleting all nodes instead of one
  • Ignoring the case when node is head or tail
2. Which of the following is the correct Java syntax to delete a node by value in a doubly linked list?
easy
A. int deleteByValue(int value) { /* code */ }
B. void deleteByValue(int value) { /* code */ }
C. deleteByValue(int value) { /* code */ }
D. void deleteByValue() { /* code */ }

Solution

  1. Step 1: Check method return type and parameters

    The method should accept an integer value to delete and return nothing, so void return type is correct.
  2. Step 2: Verify method signature syntax

    Java requires return type before method name and parameter types inside parentheses.
  3. Final Answer:

    void deleteByValue(int value) { /* code */ } -> Option B
  4. Quick Check:

    Method with void return and int parameter is correct [OK]
Hint: Method must specify return type and parameter types in Java [OK]
Common Mistakes:
  • Omitting return type
  • Missing parameter type
  • Using wrong return type for delete method
3. Given the doubly linked list: 10 <-> 20 <-> 30 <-> 40 <-> null, what is the list after deleting the node with value 30?
medium
A. 10 <-> 20 <-> 40 <-> null
B. 10 <-> 20 <-> 30 <-> 40 <-> null
C. 20 <-> 30 <-> 40 <-> null
D. 10 <-> 40 <-> null

Solution

  1. Step 1: Identify node with value 30

    Traverse the list and find the node containing 30 between 20 and 40.
  2. Step 2: Remove node 30 and update links

    Set 20's next pointer to 40 and 40's previous pointer to 20, removing 30 from the chain.
  3. Final Answer:

    10 <-> 20 <-> 40 <-> null -> Option A
  4. Quick Check:

    Node 30 removed, links updated correctly [OK]
Hint: Remove node and link previous to next nodes directly [OK]
Common Mistakes:
  • Not updating previous pointer of next node
  • Leaving deleted node linked
  • Deleting wrong node
4. What is the error in this code snippet for deleting a node by value in a doubly linked list?
void deleteByValue(int value) {
  Node current = head;
  while (current != null && current.data != value) {
    current = current.next;
  }
  if (current == null) return;
  current.prev.next = current.next;
  current.next.prev = current.prev;
}
medium
A. It reverses the list instead of deleting
B. It does not check if current is null before accessing pointers
C. It deletes all nodes with the value instead of one
D. It does not handle deletion when current is head

Solution

  1. Step 1: Analyze deletion at head node

    If current is head, current.prev is null, so current.prev.next causes NullPointerException.
  2. Step 2: Identify missing condition for head deletion

    Code must check if current.prev is null and update head pointer accordingly.
  3. Final Answer:

    It does not handle deletion when current is head -> Option D
  4. Quick Check:

    Head deletion needs special handling [OK]
Hint: Check if node is head before updating previous pointer [OK]
Common Mistakes:
  • Not handling head or tail nodes separately
  • Accessing null pointers without checks
  • Assuming node always has previous and next
5. You have a doubly linked list: 5 <-> 10 <-> 15 <-> 20 <-> 25 <-> null. You want to delete the node with value 5, then delete the node with value 25. What is the final list?
hard
A. null
B. 5 <-> 10 <-> 15 <-> 20 <-> null
C. 10 <-> 15 <-> 20 <-> null
D. 10 <-> 15 <-> 20 <-> 25 <-> null

Solution

  1. Step 1: Delete node with value 5 (head)

    Since 5 is head, update head to 10 and set 10's prev to null.
  2. Step 2: Delete node with value 25 (tail)

    Since 25 is tail, update 20's next to null, removing 25.
  3. Final Answer:

    10 <-> 15 <-> 20 <-> null -> Option C
  4. Quick Check:

    Head and tail deletions handled correctly [OK]
Hint: Update head or tail pointers when deleting first or last node [OK]
Common Mistakes:
  • Not updating head when deleting first node
  • Not updating tail when deleting last node
  • Breaking links causing list corruption
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