Challenge - 5 Problems
Delete Node Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat 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());
Attempts:
2 left
💡 Hint
Think about how the node with value 3 is removed and how the links are updated.
✗ Incorrect
The node with value 3 is removed by linking the node with value 2 directly to the node with value 4, skipping the node with value 3.
❓ Predict Output
intermediateWhat 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());
Attempts:
2 left
💡 Hint
If the value is not found, the list remains unchanged.
✗ Incorrect
Since the value 10 is not in the list, no nodes are removed and the list stays the same.
❓ Predict Output
advancedWhat 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());
Attempts:
2 left
💡 Hint
Deleting the head means the head moves to the next node.
✗ Incorrect
The head node with value 9 is removed, so the new head is the node with value 8.
🔧 Debug
advancedIdentify 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; } }
Attempts:
2 left
💡 Hint
Check the order of checking head node and traversing nodes.
✗ Incorrect
The method checks for the head node deletion after traversing, so if the head node matches, current.Next is null and accessing current.Next.Value causes NullReferenceException.
❓ Predict Output
expertWhat 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());
Attempts:
2 left
💡 Hint
All nodes with value 4 are removed, including head nodes.
✗ Incorrect
All nodes with value 4 are removed, leaving only nodes with values 5 and 6.
