Challenge - 5 Problems
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Doubly Linked List after Insertions
What is the printed state of the doubly linked list after inserting nodes with values 10, 20, and 30 at the end?
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 printList() { Node curr = head; while (curr != null) { System.out.print(curr.data); if (curr.next != null) System.out.print(" <-> "); curr = curr.next; } System.out.println(" <-> null"); } } public class Main { public static void main(String[] args) { DoublyLinkedList dll = new DoublyLinkedList(); dll.append(10); dll.append(20); dll.append(30); dll.printList(); } }
Attempts:
2 left
💡 Hint
Remember that append adds nodes at the end, so the order is the same as insertion.
✗ Incorrect
The append method adds nodes at the end. So the list after inserting 10, 20, 30 is 10 <-> 20 <-> 30 <-> null.
❓ Predict Output
intermediateState of Doubly Linked List after Deletion
What is the printed state of the doubly linked list after deleting the node with value 20 from the list 10 <-> 20 <-> 30 <-> null?
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 deleteNode(int key) { Node curr = head; while (curr != null && curr.data != key) { curr = curr.next; } if (curr == null) return; if (curr.prev != null) curr.prev.next = curr.next; else head = curr.next; if (curr.next != null) curr.next.prev = curr.prev; } void printList() { Node curr = head; while (curr != null) { System.out.print(curr.data); if (curr.next != null) System.out.print(" <-> "); curr = curr.next; } System.out.println(" <-> null"); } } public class Main { public static void main(String[] args) { DoublyLinkedList dll = new DoublyLinkedList(); dll.append(10); dll.append(20); dll.append(30); dll.deleteNode(20); dll.printList(); } }
Attempts:
2 left
💡 Hint
Deleting node 20 removes it and connects 10 directly to 30.
✗ Incorrect
After deleting node with value 20, the list links 10 directly to 30, so output is 10 <-> 30 <-> null.
🔧 Debug
advancedIdentify the Bug in Node Insertion at Head
What is the bug in the insertAtHead method below that causes incorrect doubly linked list structure?
DSA Java
class Node { int data; Node prev, next; Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { Node head; void insertAtHead(int data) { Node newNode = new Node(data); newNode.next = head; head = newNode; if (head != null) { head.prev = newNode; } } }
Attempts:
2 left
💡 Hint
Check the order of assignments and what head points to when setting prev.
✗ Incorrect
After head = newNode, head.prev = newNode sets the new node's prev to itself, which is incorrect. The prev of the old head should be set to newNode instead.
❓ Predict Output
advancedOutput after Reversing a Doubly Linked List
What is the printed state of the doubly linked list after reversing the list 1 <-> 2 <-> 3 <-> null?
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 reverse() { Node curr = head; Node temp = null; while (curr != null) { temp = curr.prev; curr.prev = curr.next; curr.next = temp; curr = curr.prev; } if (temp != null) { head = temp.prev; } } void printList() { Node curr = head; while (curr != null) { System.out.print(curr.data); if (curr.next != null) System.out.print(" <-> "); curr = curr.next; } System.out.println(" <-> null"); } } public class Main { public static void main(String[] args) { DoublyLinkedList dll = new DoublyLinkedList(); dll.append(1); dll.append(2); dll.append(3); dll.reverse(); dll.printList(); } }
Attempts:
2 left
💡 Hint
Reversing swaps next and prev pointers and updates head to last node.
✗ Incorrect
The reverse method swaps next and prev for each node and updates head to the last node, so the list becomes 3 <-> 2 <-> 1 <-> null.
🧠 Conceptual
expertMemory Usage in Doubly Linked List Node Design
Which statement correctly explains the memory overhead of a doubly linked list node compared to a singly linked list node?
Attempts:
2 left
💡 Hint
Consider how many pointers each node stores and their memory size.
✗ Incorrect
Each doubly linked list node stores two pointers (prev and next), while singly linked list nodes store only one pointer (next). This roughly doubles the pointer memory usage per node.
