Challenge - 5 Problems
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after inserting 10 at the beginning?
Consider a doubly linked list initially containing 20 -> 30 -> 40 -> null. We insert 10 at the beginning. What is the printed list?
DSA Java
class Node { int data; Node prev, next; Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { Node head; void insertAtBeginning(int data) { Node newNode = new Node(data); newNode.next = head; if (head != null) head.prev = newNode; head = newNode; } void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.print("null"); } } public class Main { public static void main(String[] args) { DoublyLinkedList dll = new DoublyLinkedList(); dll.head = new Node(20); Node second = new Node(30); Node third = new Node(40); dll.head.next = second; second.prev = dll.head; second.next = third; third.prev = second; dll.insertAtBeginning(10); dll.printList(); } }
Attempts:
2 left
💡 Hint
Remember to update the previous pointer of the old head node.
✗ Incorrect
Inserting at the beginning means the new node becomes the head. The new node's next points to the old head, and the old head's prev points back to the new node.
❓ Predict Output
intermediateWhat is the output after inserting 5 at the beginning of an empty list?
We start with an empty doubly linked list. We insert 5 at the beginning. What is the printed list?
DSA Java
class Node { int data; Node prev, next; Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { Node head; void insertAtBeginning(int data) { Node newNode = new Node(data); newNode.next = head; if (head != null) head.prev = newNode; head = newNode; } void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.print("null"); } } public class Main { public static void main(String[] args) { DoublyLinkedList dll = new DoublyLinkedList(); dll.insertAtBeginning(5); dll.printList(); } }
Attempts:
2 left
💡 Hint
Inserting into an empty list sets the new node as head with no next or prev.
✗ Incorrect
Since the list is empty, the new node becomes the head and points to null next and prev.
🔧 Debug
advancedWhy does this insertion code cause a NullPointerException?
Given the code below, inserting at the beginning sometimes causes a NullPointerException. Identify the cause.
DSA Java
void insertAtBeginning(int data) { Node newNode = new Node(data); newNode.next = head.next; if (head != null) head.prev = newNode; head = newNode; }
Attempts:
2 left
💡 Hint
Check what newNode.next should point to when inserting at beginning.
✗ Incorrect
The new node's next should point to the current head, not head.next. Setting newNode.next = head.next skips the head node and can cause null pointer errors.
🧠 Conceptual
advancedWhat pointers must be updated when inserting at the beginning of a doubly linked list?
When inserting a new node at the beginning of a doubly linked list, which pointers must be updated to maintain list integrity?
Attempts:
2 left
💡 Hint
Think about how the new node connects to the old head and how the head pointer changes.
✗ Incorrect
The new node's next points to the old head, the old head's prev points back to the new node, and the head pointer is updated to the new node.
❓ Predict Output
expertWhat is the output after multiple insertions at beginning?
Starting with an empty doubly linked list, we insert 3, then 2, then 1 at the beginning. What is the printed list?
DSA Java
class Node { int data; Node prev, next; Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { Node head; void insertAtBeginning(int data) { Node newNode = new Node(data); newNode.next = head; if (head != null) head.prev = newNode; head = newNode; } void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.print("null"); } } public class Main { public static void main(String[] args) { DoublyLinkedList dll = new DoublyLinkedList(); dll.insertAtBeginning(3); dll.insertAtBeginning(2); dll.insertAtBeginning(1); dll.printList(); } }
Attempts:
2 left
💡 Hint
Each new insertion at beginning becomes the new head.
✗ Incorrect
Inserting 3 first makes list: 3 -> null. Then inserting 2 at beginning: 2 -> 3 -> null. Then inserting 1 at beginning: 1 -> 2 -> 3 -> null.
