Challenge - 5 Problems
Doubly Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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();
Attempts:
2 left
💡 Hint
Think about how the links between nodes are updated when a node in the middle is removed.
✗ Incorrect
The node with value 3 is removed. The previous node (2) points to the next node (4), and the next node (4) points back to the previous node (2). The list prints all nodes except 3.
❓ Predict Output
intermediateOutput 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();
Attempts:
2 left
💡 Hint
Deleting the head means the head pointer must move to the next node.
✗ Incorrect
The head node with value 1 is removed. The head now points to the node with value 2. The list prints from 2 onwards.
🔧 Debug
advancedIdentify 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; } }
Attempts:
2 left
💡 Hint
Think about what happens if the node to delete is the first node.
✗ Incorrect
If the node to delete is the head, the head pointer must be updated to the next node. This code misses that step, so the head still points to the deleted node, corrupting the list.
🧠 Conceptual
advancedEffect 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?
Attempts:
2 left
💡 Hint
Consider the traversal and condition checks in the deletion method.
✗ Incorrect
The method traverses the list looking for the value. If not found, it exits without modifying any links, so the list stays the same.
❓ Predict Output
expertOutput 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();
Attempts:
2 left
💡 Hint
Check how the loop moves after deleting a node to avoid skipping nodes.
✗ Incorrect
All nodes with value 2 are removed. The list after deletion contains only nodes with values 1, 3, 4, and 5 in order.
