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 last node from doubly linked list
What is the printed state of the doubly linked list after deleting the last node?
DSA Java
class Node { int data; Node prev, next; Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { Node head; void append(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; return; } Node last = head; while (last.next != null) last = last.next; last.next = newNode; newNode.prev = last; } void deleteFromEnd() { if (head == null) return; if (head.next == null) { head = null; return; } Node last = head; while (last.next != null) last = last.next; last.prev.next = null; } void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.print("null\n"); } public static void main(String[] args) { DoublyLinkedList dll = new DoublyLinkedList(); dll.append(10); dll.append(20); dll.append(30); dll.deleteFromEnd(); dll.printList(); } }
Attempts:
2 left
💡 Hint
Deleting from end removes the last node and updates the previous node's next pointer to null.
✗ Incorrect
The list initially is 10 -> 20 -> 30 -> null. After deleting the last node (30), the list becomes 10 -> 20 -> null.
❓ Predict Output
intermediateOutput after deleting last node from single-node doubly linked list
What is the printed state of the doubly linked list after deleting the last node when the list has only one node?
DSA Java
class Node { int data; Node prev, next; Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { Node head; void append(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; return; } Node last = head; while (last.next != null) last = last.next; last.next = newNode; newNode.prev = last; } void deleteFromEnd() { if (head == null) return; if (head.next == null) { head = null; return; } Node last = head; while (last.next != null) last = last.next; last.prev.next = null; } void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.print("null\n"); } public static void main(String[] args) { DoublyLinkedList dll = new DoublyLinkedList(); dll.append(100); dll.deleteFromEnd(); dll.printList(); } }
Attempts:
2 left
💡 Hint
Deleting the only node should result in an empty list.
✗ Incorrect
Since the list has only one node, deleting from end sets head to null, resulting in an empty list.
🔧 Debug
advancedIdentify the error in deleteFromEnd method
What error will occur when running this deleteFromEnd method on a doubly linked list with multiple nodes?
DSA Java
void deleteFromEnd() {
if (head == null) return;
Node last = head;
while (last.next != null) last = last.next;
last.next = null;
}Attempts:
2 left
💡 Hint
Consider what happens to the previous node's next pointer.
✗ Incorrect
The method tries to set last.next = null, but last.next is already null. It does not update the previous node's next pointer, so the list still points to the last node. Accessing last.prev.next causes NullPointerException if last.prev is null or inconsistent state.
🧠 Conceptual
advancedWhy update previous node's next pointer when deleting last node?
In a doubly linked list, why must we update the previous node's next pointer to null when deleting the last node?
Attempts:
2 left
💡 Hint
Think about what happens if the previous node still points to the deleted node.
✗ Incorrect
If the previous node's next pointer is not set to null, it still points to the deleted node, causing dangling references and potential memory leaks.
❓ Predict Output
expertOutput after multiple deletions from end of doubly linked list
What is the printed state of the doubly linked list after deleting the last node three times in a row?
DSA Java
class Node { int data; Node prev, next; Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { Node head; void append(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; return; } Node last = head; while (last.next != null) last = last.next; last.next = newNode; newNode.prev = last; } void deleteFromEnd() { if (head == null) return; if (head.next == null) { head = null; return; } Node last = head; while (last.next != null) last = last.next; last.prev.next = null; } void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.print("null\n"); } public static void main(String[] args) { DoublyLinkedList dll = new DoublyLinkedList(); dll.append(1); dll.append(2); dll.append(3); dll.append(4); dll.append(5); dll.deleteFromEnd(); dll.deleteFromEnd(); dll.deleteFromEnd(); dll.printList(); } }
Attempts:
2 left
💡 Hint
Each deletion removes the last node, so after three deletions from a list of 5 nodes, 2 nodes remain.
✗ Incorrect
Starting with 1 -> 2 -> 3 -> 4 -> 5 -> null, deleting last node thrice removes 5, 4, and 3, leaving 1 -> 2 -> null.
