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 5 at the end?
Consider a doubly linked list initially containing nodes with values 1, 2, 3. We insert a new node with value 5 at the end. What is the printed list after this operation?
DSA Java
class Node { int data; Node prev, next; Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { Node head; void insertAtEnd(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.insertAtEnd(1); dll.insertAtEnd(2); dll.insertAtEnd(3); dll.insertAtEnd(5); dll.printList(); } }
Attempts:
2 left
💡 Hint
Remember, inserting at the end means the new node comes after the last node.
✗ Incorrect
The insertAtEnd method traverses to the last node and links the new node after it. So the list becomes 1 -> 2 -> 3 -> 5 -> null.
❓ Predict Output
intermediateWhat is the output after inserting into an empty list?
We start with an empty doubly linked list and insert a node with value 10 at the end. 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 insertAtEnd(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.insertAtEnd(10); dll.printList(); } }
Attempts:
2 left
💡 Hint
When the list is empty, the new node becomes the head.
✗ Incorrect
Since the list is empty, the new node with value 10 becomes the head and the only node. The print shows 10 -> null.
🔧 Debug
advancedWhy does this insertAtEnd method cause a NullPointerException?
This insertAtEnd method sometimes throws a NullPointerException. Identify the cause.
DSA Java
void insertAtEnd(int data) { Node newNode = new Node(data); Node temp = head; while (temp.next != null) { temp = temp.next; } temp.next = newNode; newNode.prev = temp; }
Attempts:
2 left
💡 Hint
What happens if the list is empty and head is null?
✗ Incorrect
If head is null, temp is null. Accessing temp.next causes NullPointerException. The method must handle empty list case.
🧠 Conceptual
advancedWhat is the time complexity of inserting at the end in a doubly linked list without a tail pointer?
If a doubly linked list does not maintain a tail pointer, what is the time complexity of inserting a new node at the end?
Attempts:
2 left
💡 Hint
Without a tail pointer, how do you find the last node?
✗ Incorrect
Without a tail pointer, you must start at head and traverse all nodes to reach the end, which takes O(n) time.
❓ Predict Output
expertWhat is the output after multiple insertions at the end?
Given the following code inserting 4, 7, 9 at the end of an initially empty doubly linked list, 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 insertAtEnd(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.insertAtEnd(4); dll.insertAtEnd(7); dll.insertAtEnd(9); dll.printList(); } }
Attempts:
2 left
💡 Hint
Nodes are inserted at the end, so order is preserved.
✗ Incorrect
Nodes 4, 7, 9 are inserted one after another at the end, so the list prints 4 -> 7 -> 9 -> null.
