Bird
Raised Fist0
DSA C#programming~20 mins

Delete Node by Value in DSA C# - 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?
Consider a singly linked list with nodes containing values 1 -> 2 -> 3 -> 4 -> null. We delete the node with value 3. What is the resulting list?
DSA C#
public class Node {
    public int Value;
    public Node Next;
    public Node(int value) { Value = value; Next = null; }
}

public class LinkedList {
    public Node Head;

    public void DeleteByValue(int value) {
        if (Head == null) return;
        if (Head.Value == value) {
            Head = Head.Next;
            return;
        }
        Node current = Head;
        while (current.Next != null && current.Next.Value != value) {
            current = current.Next;
        }
        if (current.Next != null) {
            current.Next = current.Next.Next;
        }
    }

    public override string ToString() {
        Node current = Head;
        string result = "";
        while (current != null) {
            result += current.Value + " -> ";
            current = current.Next;
        }
        return result + "null";
    }
}

LinkedList list = new LinkedList();
list.Head = new Node(1);
list.Head.Next = new Node(2);
list.Head.Next.Next = new Node(3);
list.Head.Next.Next.Next = new Node(4);
list.DeleteByValue(3);
Console.WriteLine(list.ToString());
A1 -> 2 -> 4 -> null
B1 -> 2 -> 3 -> 4 -> null
C1 -> 3 -> 4 -> null
D2 -> 3 -> 4 -> null
Attempts:
2 left
💡 Hint
Think about how the node with value 3 is removed and how the links are updated.
❓ Predict Output
intermediate
2:00remaining
What happens when deleting a non-existent value?
Given a linked list 5 -> 6 -> 7 -> null, what is the output after trying to delete the node with value 10?
DSA C#
public class Node {
    public int Value;
    public Node Next;
    public Node(int value) { Value = value; Next = null; }
}

public class LinkedList {
    public Node Head;

    public void DeleteByValue(int value) {
        if (Head == null) return;
        if (Head.Value == value) {
            Head = Head.Next;
            return;
        }
        Node current = Head;
        while (current.Next != null && current.Next.Value != value) {
            current = current.Next;
        }
        if (current.Next != null) {
            current.Next = current.Next.Next;
        }
    }

    public override string ToString() {
        Node current = Head;
        string result = "";
        while (current != null) {
            result += current.Value + " -> ";
            current = current.Next;
        }
        return result + "null";
    }
}

LinkedList list = new LinkedList();
list.Head = new Node(5);
list.Head.Next = new Node(6);
list.Head.Next.Next = new Node(7);
list.DeleteByValue(10);
Console.WriteLine(list.ToString());
A5 -> 7 -> null
B6 -> 7 -> null
C5 -> 6 -> 7 -> null
Dnull
Attempts:
2 left
💡 Hint
If the value is not found, the list remains unchanged.
❓ Predict Output
advanced
2:00remaining
What is the output after deleting the head node by value?
Given a linked list 9 -> 8 -> 7 -> null, what is the output after deleting the node with value 9?
DSA C#
public class Node {
    public int Value;
    public Node Next;
    public Node(int value) { Value = value; Next = null; }
}

public class LinkedList {
    public Node Head;

    public void DeleteByValue(int value) {
        if (Head == null) return;
        if (Head.Value == value) {
            Head = Head.Next;
            return;
        }
        Node current = Head;
        while (current.Next != null && current.Next.Value != value) {
            current = current.Next;
        }
        if (current.Next != null) {
            current.Next = current.Next.Next;
        }
    }

    public override string ToString() {
        Node current = Head;
        string result = "";
        while (current != null) {
            result += current.Value + " -> ";
            current = current.Next;
        }
        return result + "null";
    }
}

LinkedList list = new LinkedList();
list.Head = new Node(9);
list.Head.Next = new Node(8);
list.Head.Next.Next = new Node(7);
list.DeleteByValue(9);
Console.WriteLine(list.ToString());
A8 -> 7 -> null
B7 -> null
C9 -> 8 -> 7 -> null
Dnull
Attempts:
2 left
💡 Hint
Deleting the head means the head moves to the next node.
🔧 Debug
advanced
2:00remaining
Identify the error in this DeleteByValue method
What error will occur when running this DeleteByValue method on a linked list?
DSA C#
public void DeleteByValue(int value) {
    if (Head == null) return;
    Node current = Head;
    while (current.Next != null) {
        if (current.Next.Value == value) {
            current.Next = current.Next.Next;
            return;
        }
        current = current.Next;
    }
    if (Head.Value == value) {
        Head = Head.Next;
    }
}
ANo error; deletes node correctly
BNullReferenceException when deleting head node
CDeletes only last node always
DInfinite loop occurs
Attempts:
2 left
💡 Hint
Check the order of checking head node and traversing nodes.
❓ Predict Output
expert
3:00remaining
What is the output after deleting all nodes with a specific value?
Given a linked list 4 -> 5 -> 4 -> 6 -> 4 -> null, a method deletes all nodes with value 4. What is the resulting list?
DSA C#
public class Node {
    public int Value;
    public Node Next;
    public Node(int value) { Value = value; Next = null; }
}

public class LinkedList {
    public Node Head;

    public void DeleteAllByValue(int value) {
        while (Head != null && Head.Value == value) {
            Head = Head.Next;
        }
        Node current = Head;
        while (current != null && current.Next != null) {
            if (current.Next.Value == value) {
                current.Next = current.Next.Next;
            } else {
                current = current.Next;
            }
        }
    }

    public override string ToString() {
        Node current = Head;
        string result = "";
        while (current != null) {
            result += current.Value + " -> ";
            current = current.Next;
        }
        return result + "null";
    }
}

LinkedList list = new LinkedList();
list.Head = new Node(4);
list.Head.Next = new Node(5);
list.Head.Next.Next = new Node(4);
list.Head.Next.Next.Next = new Node(6);
list.Head.Next.Next.Next.Next = new Node(4);
list.DeleteAllByValue(4);
Console.WriteLine(list.ToString());
Anull
B4 -> 5 -> 6 -> null
C5 -> 4 -> 6 -> null
D5 -> 6 -> null
Attempts:
2 left
💡 Hint
All nodes with value 4 are removed, including head nodes.

Practice

(1/5)
1. What does the operation Delete Node by Value do in a linked list?
easy
A. Adds a new node with the given value at the end of the list
B. Finds the node with the given value and returns it without removing
C. Removes the first node that contains the given value from the list
D. Reverses the linked list

Solution

  1. Step 1: Understand the operation

    Deleting a node by value means searching for the first node that matches the value and removing it from the linked list.
  2. Step 2: Compare options

    Only Removes the first node that contains the given value from the list describes removing the first node with the given value. Others describe adding, finding without removing, or reversing, which are different operations.
  3. Final Answer:

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

    Delete Node by Value = Remove first matching node [OK]
Hint: Deleting by value removes first matching node only [OK]
Common Mistakes:
  • Confusing deletion by value with adding a node
  • Thinking it removes all nodes with that value
  • Assuming it reverses the list
2. Which of the following is the correct C# syntax to delete a node by value from a singly linked list?
easy
A. void DeleteNodeByValue(int value) { /* code */ }
B. public void DeleteNode(int value) { /* code */ }
C. public int DeleteNode(int value) { /* code */ }
D. DeleteNode(int value) => { /* code */ }

Solution

  1. Step 1: Check method signature conventions in C#

    In C#, methods usually have an access modifier like 'public' and a return type. Deleting a node usually returns void.
  2. Step 2: Analyze options

    public void DeleteNode(int value) { /* code */ } uses 'public void DeleteNode(int value)', which is correct. void DeleteNodeByValue(int value) { /* code */ } misses access modifier. public int DeleteNode(int value) { /* code */ } returns int which is uncommon here. DeleteNode(int value) => { /* code */ } uses invalid lambda syntax for method.
  3. Final Answer:

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

    Correct method signature = public void DeleteNode(int value) { /* code */ } [OK]
Hint: Use public void method with parameter for deletion [OK]
Common Mistakes:
  • Omitting access modifier
  • Using wrong return type
  • Incorrect method syntax
3. Given the linked list: 10 -> 20 -> 30 -> 40 -> null, what is the list after deleting the node with value 30 using the DeleteNodeByValue method?
medium
A. 10 -> 20 -> 40 -> null
B. 10 -> 20 -> 30 -> 40 -> null
C. 20 -> 30 -> 40 -> null
D. 10 -> 30 -> 40 -> null

Solution

  1. Step 1: Identify the node to delete

    The node with value 30 is the third node in the list.
  2. Step 2: Remove the node and link previous to next

    After deleting 30, the node with value 20 points directly to 40, so the list becomes 10 -> 20 -> 40 -> null.
  3. Final Answer:

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

    Deleted 30, list skips it [OK]
Hint: Remove node and link previous to next node [OK]
Common Mistakes:
  • Not updating links after deletion
  • Deleting wrong node
  • Leaving deleted node in list
4. Consider this code snippet to delete a node by value in a singly linked list:
public void DeleteNode(int value) {
  Node current = head;
  Node previous = null;
  while (current != null && current.data != value) {
    previous = current;
    current = current.next;
  }
  if (current == null) return;
  previous.next = current.next;
}
What error will occur when deleting the head node?
medium
A. Compilation error due to missing return type
B. No error, deletion works fine
C. Infinite loop because current never changes
D. NullReferenceException because previous is null when deleting head

Solution

  1. Step 1: Analyze deletion of head node

    If the node to delete is head, previous is still null because loop never runs.
  2. Step 2: Check assignment previous.next = current.next

    Since previous is null, trying to access previous.next causes NullReferenceException.
  3. Final Answer:

    NullReferenceException because previous is null when deleting head -> Option D
  4. Quick Check:

    Deleting head needs special case to avoid null error [OK]
Hint: Handle head deletion separately to avoid null errors [OK]
Common Mistakes:
  • Not checking if node to delete is head
  • Assuming previous is never null
  • Ignoring null pointer exceptions
5. You have a singly linked list: 5 -> 10 -> 15 -> 20 -> 25 -> null. You want to delete the node with value 5 (head), then delete the node with value 20. What will the list look like after these two deletions?
hard
A. 10 -> 15 -> 25 -> null
B. 5 -> 10 -> 15 -> 25 -> null
C. 10 -> 15 -> 20 -> 25 -> null
D. 15 -> 25 -> null

Solution

  1. Step 1: Delete head node with value 5

    Removing head means head moves to next node, so list becomes 10 -> 15 -> 20 -> 25 -> null.
  2. Step 2: Delete node with value 20

    Find node 20 and remove it by linking 15 to 25, resulting in 10 -> 15 -> 25 -> null.
  3. Final Answer:

    10 -> 15 -> 25 -> null -> Option A
  4. Quick Check:

    Deleted head and middle node correctly [OK]
Hint: Delete head first, then link previous to next for others [OK]
Common Mistakes:
  • Not updating head after deleting first node
  • Not linking nodes correctly after deletion
  • Deleting wrong nodes
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