Challenge - 5 Problems
Doubly Linked List Mastery
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 10, 20, and 30 in order?
DSA Java
class Node { int data; Node prev, next; Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { Node head; void insertEnd(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; } void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data); if (temp.next != null) System.out.print(" <-> "); temp = temp.next; } System.out.println(" <-> null"); } } public class Main { public static void main(String[] args) { DoublyLinkedList dll = new DoublyLinkedList(); dll.insertEnd(10); dll.insertEnd(20); dll.insertEnd(30); dll.printList(); } }
Attempts:
2 left
💡 Hint
Trace the insertEnd method and how nodes link forward and backward.
✗ Incorrect
Nodes are inserted at the end, so the list grows as 10 <-> 20 <-> 30. The printList method prints nodes connected by <-> and ends with <-> null.
❓ Predict Output
intermediateValue of head after initialization and insertion
After creating an empty doubly linked list and inserting one node with value 5, what is the value of head.data?
DSA Java
class Node { int data; Node prev, next; Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { Node head; void insertFront(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; return; } newNode.next = head; head.prev = newNode; head = newNode; } } public class Main { public static void main(String[] args) { DoublyLinkedList dll = new DoublyLinkedList(); dll.insertFront(5); System.out.println(dll.head.data); } }
Attempts:
2 left
💡 Hint
Check what happens when inserting the first node at the front.
✗ Incorrect
Since the list was empty, head points to the new node with data 5 after insertion.
🔧 Debug
advancedIdentify the bug in doubly linked list insertion at front
What error will this code produce when inserting nodes at the front of the doubly linked list?
DSA Java
class Node { int data; Node prev, next; Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { Node head; void insertFront(int data) { Node newNode = new Node(data); newNode.next = head; head.prev = newNode; head = newNode; } } public class Main { public static void main(String[] args) { DoublyLinkedList dll = new DoublyLinkedList(); dll.insertFront(1); } }
Attempts:
2 left
💡 Hint
Consider what happens when head is null and you try to access head.prev.
✗ Incorrect
When the list is empty, head is null. Accessing head.prev causes NullPointerException.
❓ Predict Output
advancedResult of removing a node from doubly linked list
What is the printed state of the doubly linked list after removing the node with value 20?
DSA Java
class Node { int data; Node prev, next; Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { Node head; void insertEnd(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; } void remove(int data) { Node temp = head; while (temp != null && temp.data != data) temp = temp.next; if (temp == null) return; if (temp.prev != null) temp.prev.next = temp.next; else head = temp.next; if (temp.next != null) temp.next.prev = temp.prev; } void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data); if (temp.next != null) System.out.print(" <-> "); temp = temp.next; } System.out.println(" <-> null"); } } public class Main { public static void main(String[] args) { DoublyLinkedList dll = new DoublyLinkedList(); dll.insertEnd(10); dll.insertEnd(20); dll.insertEnd(30); dll.remove(20); dll.printList(); } }
Attempts:
2 left
💡 Hint
Trace the remove method and how links are updated.
✗ Incorrect
Node with value 20 is removed, so list becomes 10 <-> 30 <-> null.
🧠 Conceptual
expertWhy is a doubly linked list preferred over singly linked list for certain operations?
Which of the following is the main advantage of a doubly linked list compared to a singly linked list?
Attempts:
2 left
💡 Hint
Think about how nodes link to neighbors in both list types.
✗ Incorrect
Doubly linked lists have pointers to both previous and next nodes, enabling efficient two-way traversal.
