Challenge - 5 Problems
Doubly Linked List Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting at position 3
What is the printed state of the doubly linked list after inserting the value 25 at position 3?
DSA Java
class Node { int data; Node prev, next; Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { Node head; void insertAtPosition(int data, int position) { Node newNode = new Node(data); if (position == 1) { newNode.next = head; if (head != null) head.prev = newNode; head = newNode; return; } Node temp = head; for (int i = 1; i < position - 1 && temp != null; i++) { temp = temp.next; } if (temp == null) return; newNode.next = temp.next; if (temp.next != null) temp.next.prev = newNode; 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(); } public static void main(String[] args) { DoublyLinkedList dll = new DoublyLinkedList(); dll.insertAtPosition(10, 1); dll.insertAtPosition(20, 2); dll.insertAtPosition(30, 3); dll.insertAtPosition(25, 3); dll.printList(); } }
Attempts:
2 left
💡 Hint
Remember that inserting at position 3 means the new node should be placed between nodes at positions 2 and 3.
✗ Incorrect
The list starts as 10 <-> 20 <-> 30. Inserting 25 at position 3 places it between 20 and 30, resulting in 10 <-> 20 <-> 25 <-> 30.
❓ Predict Output
intermediateOutput after inserting at head position
What is the printed state of the doubly linked list after inserting the value 5 at position 1?
DSA Java
class Node { int data; Node prev, next; Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { Node head; void insertAtPosition(int data, int position) { Node newNode = new Node(data); if (position == 1) { newNode.next = head; if (head != null) head.prev = newNode; head = newNode; return; } Node temp = head; for (int i = 1; i < position - 1 && temp != null; i++) { temp = temp.next; } if (temp == null) return; newNode.next = temp.next; if (temp.next != null) temp.next.prev = newNode; 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(); } public static void main(String[] args) { DoublyLinkedList dll = new DoublyLinkedList(); dll.insertAtPosition(10, 1); dll.insertAtPosition(20, 2); dll.insertAtPosition(30, 3); dll.insertAtPosition(5, 1); dll.printList(); } }
Attempts:
2 left
💡 Hint
Inserting at position 1 means the new node becomes the new head.
✗ Incorrect
The new node with value 5 is inserted at the head, pushing the existing nodes forward.
🔧 Debug
advancedIdentify the bug causing NullPointerException
Why does the following code cause a NullPointerException when inserting at position 4 in a list of size 3?
DSA Java
void insertAtPosition(int data, int position) { Node newNode = new Node(data); if (position == 1) { newNode.next = head; if (head != null) head.prev = newNode; head = newNode; return; } Node temp = head; for (int i = 1; i < position - 1; i++) { temp = temp.next; } newNode.next = temp.next; if (temp.next != null) temp.next.prev = newNode; temp.next = newNode; newNode.prev = temp; }
Attempts:
2 left
💡 Hint
Check the loop condition and what happens if position is greater than list size + 1.
✗ Incorrect
If position is beyond the list size + 1, temp becomes null in the loop, and accessing temp.next causes NullPointerException.
❓ Predict Output
advancedOutput after inserting at tail position
What is the printed state of the doubly linked list after inserting the value 40 at position 5 in the list 10 <-> 20 <-> 30 <-> 25?
DSA Java
class Node { int data; Node prev, next; Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { Node head; void insertAtPosition(int data, int position) { Node newNode = new Node(data); if (position == 1) { newNode.next = head; if (head != null) head.prev = newNode; head = newNode; return; } Node temp = head; for (int i = 1; i < position - 1 && temp != null; i++) { temp = temp.next; } if (temp == null) return; newNode.next = temp.next; if (temp.next != null) temp.next.prev = newNode; 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(); } public static void main(String[] args) { DoublyLinkedList dll = new DoublyLinkedList(); dll.insertAtPosition(10, 1); dll.insertAtPosition(20, 2); dll.insertAtPosition(30, 3); dll.insertAtPosition(25, 4); dll.insertAtPosition(40, 5); dll.printList(); } }
Attempts:
2 left
💡 Hint
Inserting at position 5 means adding after the current last node (position 4).
✗ Incorrect
The new node 40 is added after node 25, which is at position 4, so it becomes the new tail.
🧠 Conceptual
expertTime complexity of insertion at specific position
What is the time complexity of inserting a node at a specific position in a doubly linked list of size n, assuming you have only the head pointer?
Attempts:
2 left
💡 Hint
Think about how you find the node before the insertion point.
✗ Incorrect
Without direct access to the position, you must traverse nodes from the head, which takes linear time.
