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 the first node from a doubly linked list
What is the printed state of the doubly linked list after deleting the first node?
DSA C#
using System; class Node { public int data; public Node prev, next; public Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { public Node head; public void Push(int new_data) { Node new_node = new Node(new_data); new_node.next = head; if (head != null) head.prev = new_node; head = new_node; } public void DeleteFromBeginning() { if (head == null) return; head = head.next; if (head != null) head.prev = null; } public void PrintList() { Node temp = head; while (temp != null) { Console.Write(temp.data + " <-> "); temp = temp.next; } Console.WriteLine("null"); } } class Program { static void Main() { DoublyLinkedList dll = new DoublyLinkedList(); dll.Push(3); dll.Push(2); dll.Push(1); dll.DeleteFromBeginning(); dll.PrintList(); } }
Attempts:
2 left
💡 Hint
Remember that deleting from the beginning removes the head node and updates the head pointer.
✗ Incorrect
Initially, the list is 1 <-> 2 <-> 3 <-> null after pushing 1, 2, 3. Deleting from the beginning removes 1, so the new head is 2, resulting in 2 <-> 3 <-> null.
❓ Predict Output
intermediateResulting list after deleting from an empty doubly linked list
What is the output when deleting from the beginning of an empty doubly linked list and then printing it?
DSA C#
using System; class Node { public int data; public Node prev, next; public Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { public Node head; public void DeleteFromBeginning() { if (head == null) return; head = head.next; if (head != null) head.prev = null; } public void PrintList() { Node temp = head; while (temp != null) { Console.Write(temp.data + " <-> "); temp = temp.next; } Console.WriteLine("null"); } } class Program { static void Main() { DoublyLinkedList dll = new DoublyLinkedList(); dll.DeleteFromBeginning(); dll.PrintList(); } }
Attempts:
2 left
💡 Hint
Check what happens if the list is empty before deletion.
✗ Incorrect
Since the list is empty (head is null), deleting from beginning does nothing. Printing the list shows 'null' indicating empty list.
🔧 Debug
advancedIdentify the error in deleting the first node of a doubly linked list
What error does the following code produce when deleting the first node from a doubly linked list?
DSA C#
public void DeleteFromBeginning() {
if (head == null) return;
head = head.next;
head.prev = null;
}Attempts:
2 left
💡 Hint
Consider the order of operations and what happens when head.next is null.
✗ Incorrect
If the list has only one node, head.next is null. After head = head.next, head becomes null. Then attempting head.prev = null causes NullReferenceException since head is null.
🧠 Conceptual
advancedEffect of deleting the first node on the previous pointer of the new head
After deleting the first node from a doubly linked list, what should be the value of the previous pointer of the new head node?
Attempts:
2 left
💡 Hint
Think about the start of the list and what previous means for the head node.
✗ Incorrect
The head node is the first node in the list, so its previous pointer must be null to indicate no node before it.
❓ Predict Output
expertOutput after multiple deletions from the beginning of a doubly linked list
What is the printed state of the doubly linked list after deleting the first node twice in a row?
DSA C#
using System; class Node { public int data; public Node prev, next; public Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { public Node head; public void Push(int new_data) { Node new_node = new Node(new_data); new_node.next = head; if (head != null) head.prev = new_node; head = new_node; } public void DeleteFromBeginning() { if (head == null) return; head = head.next; if (head != null) head.prev = null; } public void PrintList() { Node temp = head; while (temp != null) { Console.Write(temp.data + " <-> "); temp = temp.next; } Console.WriteLine("null"); } } class Program { static void Main() { DoublyLinkedList dll = new DoublyLinkedList(); dll.Push(4); dll.Push(3); dll.Push(2); dll.Push(1); dll.DeleteFromBeginning(); dll.DeleteFromBeginning(); dll.PrintList(); } }
Attempts:
2 left
💡 Hint
Each deletion removes the current head and updates the head pointer.
✗ Incorrect
Initially, the list is 1 <-> 2 <-> 3 <-> 4 <-> null after pushing 1,2,3,4. First deletion removes 1, new head is 2. Second deletion removes 2, new head is 3. So the list is 3 <-> 4 <-> null.
