Bird
Raised Fist0
DSA C#programming~20 mins

Delete by Value in Doubly Linked List in DSA C# - 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 middle value from doubly linked list
What is the printed state of the doubly linked list after deleting the value 3?
DSA C#
public class Node {
    public int data;
    public Node prev, next;
    public Node(int d) { data = d; prev = next = null; }
}

public class DoublyLinkedList {
    public Node head;

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

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

    public void PrintList() {
        Node temp = head;
        while (temp != null) {
            System.Console.Write(temp.data + " -> ");
            temp = temp.next;
        }
        System.Console.WriteLine("null");
    }
}

var dll = new DoublyLinkedList();
dll.Append(1);
dll.Append(2);
dll.Append(3);
dll.Append(4);
dll.Append(5);
dll.DeleteByValue(3);
dll.PrintList();
A1 -> 2 -> 4 -> null
B1 -> 2 -> 4 -> 5 -> null
C2 -> 3 -> 4 -> 5 -> null
D1 -> 2 -> 3 -> 4 -> 5 -> null
Attempts:
2 left
💡 Hint
Think about how the links between nodes are updated when a node in the middle is removed.
❓ Predict Output
intermediate
2:00remaining
Output after deleting the head node by value
What is the printed state of the doubly linked list after deleting the value 1 (head node)?
DSA C#
public class Node {
    public int data;
    public Node prev, next;
    public Node(int d) { data = d; prev = next = null; }
}

public class DoublyLinkedList {
    public Node head;

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

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

    public void PrintList() {
        Node temp = head;
        while (temp != null) {
            System.Console.Write(temp.data + " -> ");
            temp = temp.next;
        }
        System.Console.WriteLine("null");
    }
}

var dll = new DoublyLinkedList();
dll.Append(1);
dll.Append(2);
dll.Append(3);
dll.Append(4);
dll.Append(5);
dll.DeleteByValue(1);
dll.PrintList();
A2 -> 3 -> 4 -> 5 -> null
B1 -> 2 -> 3 -> 4 -> 5 -> null
C1 -> 3 -> 4 -> 5 -> null
D2 -> 3 -> 4 -> null
Attempts:
2 left
💡 Hint
Deleting the head means the head pointer must move to the next node.
🔧 Debug
advanced
2:00remaining
Identify the bug causing incorrect deletion in doubly linked list
Given the following DeleteByValue method, what is the bug that causes the list to become corrupted after deleting a node?
DSA C#
public void DeleteByValue(int value) {
    Node temp = head;
    while (temp != null) {
        if (temp.data == value) {
            if (temp.prev != null) temp.prev.next = temp.next;
            if (temp.next != null) temp.next.prev = temp.prev;
            break;
        }
        temp = temp.next;
    }
}
AThe next pointer of the previous node is not updated correctly.
BThe loop does not break after deletion causing multiple deletions.
CThe prev pointer of the next node is not updated correctly.
DThe head pointer is not updated when deleting the first node.
Attempts:
2 left
💡 Hint
Think about what happens if the node to delete is the first node.
🧠 Conceptual
advanced
1:00remaining
Effect of deleting a non-existent value in doubly linked list
What happens to the doubly linked list if you try to delete a value that does not exist in the list?
AThe list remains unchanged and no nodes are deleted.
BThe last node in the list is deleted by mistake.
CThe head node is deleted regardless of the value.
DThe list becomes empty.
Attempts:
2 left
💡 Hint
Consider the traversal and condition checks in the deletion method.
❓ Predict Output
expert
3:00remaining
Output after deleting all occurrences of a value in doubly linked list
What is the printed state of the doubly linked list after deleting all nodes with value 2?
DSA C#
public class Node {
    public int data;
    public Node prev, next;
    public Node(int d) { data = d; prev = next = null; }
}

public class DoublyLinkedList {
    public Node head;

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

    public void DeleteAllByValue(int value) {
        Node temp = head;
        while (temp != null) {
            if (temp.data == value) {
                Node toDelete = temp;
                if (toDelete.prev != null) toDelete.prev.next = toDelete.next;
                else head = toDelete.next;
                if (toDelete.next != null) toDelete.next.prev = toDelete.prev;
                temp = toDelete.next;
            } else {
                temp = temp.next;
            }
        }
    }

    public void PrintList() {
        Node temp = head;
        while (temp != null) {
            System.Console.Write(temp.data + " -> ");
            temp = temp.next;
        }
        System.Console.WriteLine("null");
    }
}

var dll = new DoublyLinkedList();
dll.Append(1);
dll.Append(2);
dll.Append(3);
dll.Append(2);
dll.Append(4);
dll.Append(2);
dll.Append(5);
dll.DeleteAllByValue(2);
dll.PrintList();
A1 -> 3 -> 2 -> 4 -> 5 -> null
B1 -> 3 -> 4 -> 2 -> 5 -> null
C1 -> 3 -> 4 -> 5 -> null
D2 -> 3 -> 4 -> 5 -> null
Attempts:
2 left
💡 Hint
Check how the loop moves after deleting a node to avoid skipping nodes.

Practice

(1/5)
1. What happens when you delete a node by value in a doubly linked list if the value is not found?
easy
A. An error is thrown and the program stops.
B. The first node is deleted regardless of the value.
C. All nodes are deleted from the list.
D. The list remains unchanged because no node matches the value.

Solution

  1. Step 1: Search for the node with the given value

    Traverse the list from head to tail to find a node matching the value.
  2. Step 2: Check if node is found

    If no node matches the value, no deletion occurs and the list stays the same.
  3. Final Answer:

    The list remains unchanged because no node matches the value. -> Option D
  4. Quick Check:

    Delete by value with no match = no change [OK]
Hint: If value not found, list stays same [OK]
Common Mistakes:
  • Assuming deletion happens even if value not found
  • Trying to delete head without checking value
  • Expecting an error instead of no action
2. Which of the following is the correct way to update the previous pointer when deleting a middle node in a doubly linked list in C#?
easy
A. nodeToDelete.Next.Prev = nodeToDelete.Prev;
B. nodeToDelete.Prev.Next = nodeToDelete.Next.Prev;
C. nodeToDelete.Next = nodeToDelete.Prev.Next;
D. nodeToDelete.Prev = nodeToDelete.Next.Prev;

Solution

  1. Step 1: Understand pointers in doubly linked list

    When deleting a node, the next node's previous pointer must point to the deleted node's previous node.
  2. Step 2: Correct pointer update

    Setting nodeToDelete.Next.Prev = nodeToDelete.Prev correctly links the next node back to the previous node.
  3. Final Answer:

    nodeToDelete.Next.Prev = nodeToDelete.Prev; -> Option A
  4. Quick Check:

    Update next node's Prev to deleted node's Prev [OK]
Hint: Next node's Prev points to deleted node's Prev [OK]
Common Mistakes:
  • Mixing up Next and Prev pointers
  • Assigning Prev to Next's Prev incorrectly
  • Not updating both Next and Prev pointers
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 <-> 30 <-> 40 <-> null
B. 10 <-> 20 <-> 40 <-> null
C. 20 <-> 30 <-> 40 <-> null
D. 10 <-> 30 <-> 40 <-> null

Solution

  1. Step 1: Locate 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 pointers

    Set 20's Next to 40 and 40's Prev to 20, skipping 30.
  3. Final Answer:

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

    Deleted 30, linked 20 and 40 directly [OK]
Hint: Remove node, link previous and next nodes [OK]
Common Mistakes:
  • Not updating both Prev and Next pointers
  • Leaving deleted node in the list
  • Deleting wrong node by mistake
4. Consider this code snippet for deleting a node by value in a doubly linked list:
if (current == null) return;
if (current.Prev != null) current.Prev.Next = current.Next;
if (current.Next != null) current.Next.Prev = current.Prev;
if (current == head) head = current.Next;
What is the bug in this code?
medium
A. It may cause a null reference if current is null before checks.
B. It does not update the head when deleting the first node.
C. It does not handle deleting the last node correctly.
D. It does not remove the node from memory.

Solution

  1. Step 1: Verify null safety

    The initial check if (current == null) return; and conditional pointer accesses prevent null references.
  2. Step 2: Check head and pointer updates

    Head is updated correctly when deleting the first node, and pointers are linked properly for middle nodes.
  3. Step 3: Identify the issue with last node

    The code sets current.Prev.Next = null but does not update a potential tail pointer to current.Prev, leaving the tail pointing to the deleted node.
  4. Final Answer:

    It does not handle deleting the last node correctly. -> Option C
  5. Quick Check:

    Missing tail update for last node deletion [OK]
Hint: Update tail pointer when deleting last node [OK]
Common Mistakes:
  • Ignoring null checks before pointer access
  • Not updating head when deleting first node
  • Assuming garbage collection removes node immediately
5. You have a doubly linked list with nodes: 5 <-> 10 <-> 15 <-> 20 <-> 25 <-> null. You want to delete all nodes with values greater than 15. Which approach correctly deletes these nodes by value?
hard
A. Traverse from head, delete nodes with value > 15, updating pointers carefully.
B. Delete nodes by value without updating pointers, then reset head.
C. Only delete the first node with value > 15 and stop.
D. Traverse from tail, delete nodes with value < 15.

Solution

  1. Step 1: Traverse list from head to tail

    Visit each node and check if its value is greater than 15.
  2. Step 2: Delete matching nodes and update pointers

    For each node with value > 15, remove it by linking its previous node to its next node, updating both Next and Prev pointers.
  3. Step 3: Continue traversal carefully

    After deletion, continue traversal without skipping nodes to delete all matching nodes.
  4. Final Answer:

    Traverse from head, delete nodes with value > 15, updating pointers carefully. -> Option A
  5. Quick Check:

    Delete all matching nodes with pointer updates [OK]
Hint: Traverse and delete nodes carefully, update pointers [OK]
Common Mistakes:
  • Deleting nodes without updating pointers
  • Stopping after first deletion
  • Deleting wrong nodes by incorrect condition
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