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 value from doubly linked list
What is the printed state of the doubly linked list after deleting the value 3?
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 deleteByValue(int value) { Node current = head; while (current != null) { if (current.data == value) { if (current.prev != null) current.prev.next = current.next; else head = current.next; if (current.next != null) current.next.prev = current.prev; break; } current = current.next; } } void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.print("null\n"); } } public class Main { 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.deleteByValue(3); dll.printList(); } }
Attempts:
2 left
💡 Hint
Focus on how the deleteByValue method updates the links when the node with value 3 is found.
✗ Incorrect
The deleteByValue method searches for the node with data 3 and removes it by adjusting the previous node's next pointer and the next node's prev pointer. The list after deletion is 1 -> 2 -> 4 -> 5 -> null.
🧠 Conceptual
intermediateUnderstanding node deletion in doubly linked list
When deleting a node by value in a doubly linked list, which pointers must be updated to maintain list integrity?
Attempts:
2 left
💡 Hint
Think about how nodes are connected in both directions.
✗ Incorrect
In a doubly linked list, each node points to both previous and next nodes. When deleting a node, you must update the previous node's next pointer and the next node's prev pointer to bypass the deleted node.
🔧 Debug
advancedIdentify the error in deleteByValue method
What error will occur when running this deleteByValue method if the value to delete is not found in the list?
DSA Java
void deleteByValue(int value) { Node current = head; while (current != null) { if (current.data == value) { if (current.prev != null) current.prev.next = current.next; else head = current.next; if (current.next != null) current.next.prev = current.prev; break; } current = current.next; } System.out.println(current.data); }
Attempts:
2 left
💡 Hint
Consider what happens if the value is not found and current becomes null.
✗ Incorrect
If the value is not found, current becomes null after the loop. Then accessing current.data causes a NullPointerException.
❓ Predict Output
advancedOutput after deleting head node by value
What is the printed state of the doubly linked list after deleting the value 1 (head 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 deleteByValue(int value) { Node current = head; while (current != null) { if (current.data == value) { if (current.prev != null) current.prev.next = current.next; else head = current.next; if (current.next != null) current.next.prev = current.prev; break; } current = current.next; } } void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.print("null\n"); } } public class Main { public static void main(String[] args) { DoublyLinkedList dll = new DoublyLinkedList(); dll.append(1); dll.append(2); dll.append(3); dll.deleteByValue(1); dll.printList(); } }
Attempts:
2 left
💡 Hint
Deleting the head node updates the head pointer to the next node.
✗ Incorrect
Since the head node with value 1 is deleted, the head pointer moves to the node with value 2. The list becomes 2 -> 3 -> null.
🧠 Conceptual
expertWhy is doubly linked list deletion by value more efficient than singly linked list?
Which reason best explains why deleting a node by value in a doubly linked list can be more efficient than in a singly linked list?
Attempts:
2 left
💡 Hint
Think about how the previous node is found in singly vs doubly linked lists.
✗ Incorrect
In singly linked lists, to delete a node by value, you must traverse to find the previous node to update its next pointer. In doubly linked lists, each node has a prev pointer, so you can update links directly without extra traversal.
