Challenge - 5 Problems
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting nodes at the beginning
What is the printed state of the doubly linked list after inserting 10, then 20, then 30 at the beginning?
DSA C#
public class Node { public int data; public Node prev, next; public Node(int d) { data = d; prev = next = null; } } public class DoublyLinkedList { public Node head; public void InsertAtBeginning(int newData) { Node newNode = new Node(newData); newNode.next = head; if (head != null) { head.prev = newNode; } head = newNode; } public void PrintList() { Node temp = head; while (temp != null) { System.Console.Write(temp.data + " <-> "); temp = temp.next; } System.Console.WriteLine("null"); } } class Program { static void Main() { DoublyLinkedList dll = new DoublyLinkedList(); dll.InsertAtBeginning(10); dll.InsertAtBeginning(20); dll.InsertAtBeginning(30); dll.PrintList(); } }
Attempts:
2 left
💡 Hint
Remember, inserting at the beginning means the newest node becomes the head.
✗ Incorrect
Each insertion adds a new node at the start. So the last inserted node (30) is the head, followed by 20, then 10.
🧠 Conceptual
intermediateUnderstanding pointer updates in insertion
When inserting a new node at the beginning of a doubly linked list, which pointers must be updated?
Attempts:
2 left
💡 Hint
Think about how the new node connects forward and backward.
✗ Incorrect
The new node's next must point to the old head. The old head's prev must point back to the new node. Finally, the head pointer is updated to the new node.
🔧 Debug
advancedIdentify the bug in insertion code
What error will this code cause when inserting at the beginning of a doubly linked list?
public void InsertAtBeginning(int newData) {
Node newNode = new Node(newData);
newNode.prev = head;
head.next = newNode;
head = newNode;
}
Attempts:
2 left
💡 Hint
Consider what happens if the list is empty (head is null).
✗ Incorrect
If head is null, then head.next causes a NullReferenceException because you cannot access next on null.
❓ Predict Output
advancedOutput after multiple insertions and one removal
Given this sequence of operations, what is the final printed list?
InsertAtBeginning(5)
InsertAtBeginning(15)
InsertAtBeginning(25)
RemoveFirst()
PrintList()
DSA C#
public class Node { public int data; public Node prev, next; public Node(int d) { data = d; prev = next = null; } } public class DoublyLinkedList { public Node head; public void InsertAtBeginning(int newData) { Node newNode = new Node(newData); newNode.next = head; if (head != null) { head.prev = newNode; } head = newNode; } public void RemoveFirst() { if (head == null) return; head = head.next; if (head != null) { head.prev = null; } } public void PrintList() { Node temp = head; while (temp != null) { System.Console.Write(temp.data + " <-> "); temp = temp.next; } System.Console.WriteLine("null"); } } class Program { static void Main() { DoublyLinkedList dll = new DoublyLinkedList(); dll.InsertAtBeginning(5); dll.InsertAtBeginning(15); dll.InsertAtBeginning(25); dll.RemoveFirst(); dll.PrintList(); } }
Attempts:
2 left
💡 Hint
Removing first removes the head node.
✗ Incorrect
After inserting 25, 15, 5 at beginning, list is 25 <-> 15 <-> 5. Removing first removes 25, so list becomes 15 <-> 5.
🧠 Conceptual
expertTime complexity of insertion at beginning
What is the time complexity of inserting a node at the beginning of a doubly linked list and why?
Attempts:
2 left
💡 Hint
Think about how many nodes you need to visit to insert at the start.
✗ Incorrect
Insertion at the beginning only updates the head pointer and adjacent nodes, so it takes constant time.
