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 4 at the end?
Given the doubly linked list 1 <-> 2 <-> 3 <-> null, what will be the printed list after inserting 4 at the end?
DSA C#
class Node { public int data; public Node next; public Node prev; public Node(int d) { data = d; next = null; prev = null; } } class DoublyLinkedList { public Node head; public 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; } public void PrintList() { Node temp = head; while (temp != null) { System.Console.Write(temp.data); if (temp.next != null) System.Console.Write(" <-> "); temp = temp.next; } System.Console.WriteLine(" -> null"); } } DoublyLinkedList dll = new DoublyLinkedList(); dll.InsertAtEnd(1); dll.InsertAtEnd(2); dll.InsertAtEnd(3); dll.InsertAtEnd(4); 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 adds the new node after it, updating the previous pointer of the new node. The printed list shows all nodes connected with <-> and ends with -> null.
❓ Predict Output
intermediateWhat is the output after inserting 10 at the end of an empty list?
Given an empty doubly linked list, what will be the printed list after inserting 10 at the end?
DSA C#
class Node { public int data; public Node next; public Node prev; public Node(int d) { data = d; next = null; prev = null; } } class DoublyLinkedList { public Node head; public 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; } public void PrintList() { Node temp = head; while (temp != null) { System.Console.Write(temp.data); if (temp.next != null) System.Console.Write(" <-> "); temp = temp.next; } System.Console.WriteLine(" -> null"); } } 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 data 10 becomes the head. The print shows '10 -> null' because there is only one node.
🔧 Debug
advancedWhat error occurs when inserting at end if prev pointer is not set?
Consider this InsertAtEnd method missing the line that sets newNode.prev = temp. What error or problem will occur when traversing backwards from the last node?
DSA C#
public 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; // Missing: newNode.prev = temp; }
Attempts:
2 left
💡 Hint
Think about what happens if the previous pointer is not set for the new node.
✗ Incorrect
Without setting newNode.prev, the backward link is broken. Traversing backwards from the last node will try to access a null prev pointer, causing a NullReferenceException.
🧠 Conceptual
advancedWhy is it necessary to traverse to the end before inserting?
In a doubly linked list, why do we need to traverse from head to the last node before inserting a new node at the end?
Attempts:
2 left
💡 Hint
Think about what information the list stores about its nodes.
✗ Incorrect
A doubly linked list typically only stores a reference to the head node. To insert at the end, we must find the last node by traversing from the head.
❓ Predict Output
expertWhat is the output after multiple insertions and one deletion?
Given the code below, what is the printed list after inserting 5, 10, 15 at the end and then deleting the node with data 10?
DSA C#
class Node { public int data; public Node next; public Node prev; public Node(int d) { data = d; next = null; prev = null; } } class DoublyLinkedList { public Node head; public 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; } public void DeleteNode(int data) { Node temp = head; while (temp != null && temp.data != data) { temp = temp.next; } if (temp == null) return; // Not found if (temp.prev != null) temp.prev.next = temp.next; else head = temp.next; if (temp.next != null) temp.next.prev = temp.prev; } public void PrintList() { Node temp = head; while (temp != null) { System.Console.Write(temp.data); if (temp.next != null) System.Console.Write(" <-> "); temp = temp.next; } System.Console.WriteLine(" -> null"); } } DoublyLinkedList dll = new DoublyLinkedList(); dll.InsertAtEnd(5); dll.InsertAtEnd(10); dll.InsertAtEnd(15); dll.DeleteNode(10); dll.PrintList();
Attempts:
2 left
💡 Hint
Deleting 10 removes that node and links 5 directly to 15.
✗ Incorrect
After deleting the node with data 10, the list connects 5 directly to 15. The print shows '5 <-> 15 -> null'.
