Challenge - 5 Problems
Doubly Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after deleting the last node?
Consider a doubly linked list with nodes 10 <-> 20 <-> 30 <-> 40. After deleting the last node, what is the printed list?
DSA C#
class Node { public int data; public Node next; public Node prev; public Node(int d) { data = d; next = null; prev = null; } } 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 DeleteFromEnd() { if (head == null) return; if (head.next == null) { head = null; return; } Node temp = head; while (temp.next != null) { temp = temp.next; } temp.prev.next = null; } public void PrintList() { Node temp = head; while (temp != null) { System.Console.Write(temp.data); if (temp.next != null) System.Console.Write(" <-> "); temp = temp.next; } System.Console.WriteLine(); } } var dll = new DoublyLinkedList(); dll.Append(10); dll.Append(20); dll.Append(30); dll.Append(40); dll.DeleteFromEnd(); dll.PrintList();
Attempts:
2 left
💡 Hint
Deleting from end removes the last node and updates the previous node's next pointer.
✗ Incorrect
The last node with value 40 is removed. The previous node 30's next pointer is set to null, so the list ends at 30.
❓ Predict Output
intermediateWhat happens when deleting from an empty doubly linked list?
Given an empty doubly linked list, what is the output after calling DeleteFromEnd() and then printing the list?
DSA C#
class Node { public int data; public Node next; public Node prev; public Node(int d) { data = d; next = null; prev = null; } } class DoublyLinkedList { public Node head; public void DeleteFromEnd() { if (head == null) return; if (head.next == null) { head = null; return; } Node temp = head; while (temp.next != null) { temp = temp.next; } temp.prev.next = null; } public void PrintList() { Node temp = head; if (temp == null) { System.Console.WriteLine("List is empty"); return; } while (temp != null) { System.Console.Write(temp.data); if (temp.next != null) System.Console.Write(" <-> "); temp = temp.next; } System.Console.WriteLine(); } } var dll = new DoublyLinkedList(); dll.DeleteFromEnd(); dll.PrintList();
Attempts:
2 left
💡 Hint
Check if the list is empty before deleting.
✗ Incorrect
Since the list is empty, DeleteFromEnd does nothing and PrintList prints 'List is empty'.
🔧 Debug
advancedIdentify the bug in DeleteFromEnd method
What is the bug in the following DeleteFromEnd method of a doubly linked list?
DSA C#
public void DeleteFromEnd() {
if (head == null) return;
if (head.next == null) {
head = null;
return;
}
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = null;
}Attempts:
2 left
💡 Hint
Think about how to remove the last node and update the previous node's next pointer.
✗ Incorrect
The code tries to set temp.next = null, but temp.next is already null (last node). It should update temp.prev.next to null to remove the last node.
🧠 Conceptual
advancedWhat is the time complexity of deleting from the end of a doubly linked list without tail pointer?
If a doubly linked list does not maintain a tail pointer, what is the time complexity of deleting the last node?
Attempts:
2 left
💡 Hint
Without a tail pointer, you must start from head and go to the end.
✗ Incorrect
Without a tail pointer, you must traverse all nodes to reach the last node, which takes O(n) time.
🚀 Application
expertAfter multiple deletions from end, what is the final list?
Starting with a doubly linked list: 5 <-> 15 <-> 25 <-> 35 <-> 45, after calling DeleteFromEnd() three times, what is the printed list?
DSA C#
class Node { public int data; public Node next; public Node prev; public Node(int d) { data = d; next = null; prev = null; } } 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 DeleteFromEnd() { if (head == null) return; if (head.next == null) { head = null; return; } Node temp = head; while (temp.next != null) { temp = temp.next; } temp.prev.next = null; } public void PrintList() { Node temp = head; while (temp != null) { System.Console.Write(temp.data); if (temp.next != null) System.Console.Write(" <-> "); temp = temp.next; } System.Console.WriteLine(); } } var dll = new DoublyLinkedList(); dll.Append(5); dll.Append(15); dll.Append(25); dll.Append(35); dll.Append(45); dll.DeleteFromEnd(); dll.DeleteFromEnd(); dll.DeleteFromEnd(); dll.PrintList();
Attempts:
2 left
💡 Hint
Each deletion removes the last node, so after three deletions, three nodes are removed.
✗ Incorrect
The last three nodes 45, 35, and 25 are removed. Remaining nodes are 5 and 15.
