Challenge - 5 Problems
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Adding Nodes to Doubly Linked List
What is the printed state of the doubly linked list after adding three nodes with values 10, 20, and 30 in that order?
DSA C#
public class Node { public int Value; public Node Prev; public Node Next; public Node(int value) { Value = value; Prev = null; Next = null; } } public class DoublyLinkedList { public Node Head; public Node Tail; public void AddLast(int value) { Node newNode = new Node(value); if (Head == null) { Head = Tail = newNode; } else { Tail.Next = newNode; newNode.Prev = Tail; Tail = newNode; } } public void PrintList() { Node current = Head; while (current != null) { System.Console.Write(current.Value); if (current.Next != null) System.Console.Write(" <-> "); current = current.Next; } System.Console.WriteLine(" <-> null"); } } var list = new DoublyLinkedList(); list.AddLast(10); list.AddLast(20); list.AddLast(30); list.PrintList();
Attempts:
2 left
💡 Hint
Think about how nodes are added at the end and how the list is printed from head to tail.
✗ Incorrect
Nodes are added at the end, so the list order is 10, then 20, then 30. Printing starts from head, so output is 10 <-> 20 <-> 30 <-> null.
❓ Predict Output
intermediateOutput After Removing a Node from Doubly Linked List
What is the printed state of the doubly linked list after removing the node with value 20 from the list 10 <-> 20 <-> 30 <-> null?
DSA C#
public class Node { public int Value; public Node Prev; public Node Next; public Node(int value) { Value = value; Prev = null; Next = null; } } public class DoublyLinkedList { public Node Head; public Node Tail; public void AddLast(int value) { Node newNode = new Node(value); if (Head == null) { Head = Tail = newNode; } else { Tail.Next = newNode; newNode.Prev = Tail; Tail = newNode; } } public void Remove(int value) { Node current = Head; while (current != null) { if (current.Value == value) { if (current.Prev != null) current.Prev.Next = current.Next; else Head = current.Next; if (current.Next != null) current.Next.Prev = current.Prev; else Tail = current.Prev; break; } current = current.Next; } } public void PrintList() { Node current = Head; while (current != null) { System.Console.Write(current.Value); if (current.Next != null) System.Console.Write(" <-> "); current = current.Next; } System.Console.WriteLine(" <-> null"); } } var list = new DoublyLinkedList(); list.AddLast(10); list.AddLast(20); list.AddLast(30); list.Remove(20); list.PrintList();
Attempts:
2 left
💡 Hint
Removing a node updates the previous and next nodes to skip the removed node.
✗ Incorrect
Node with value 20 is removed, so 10's next points to 30 and 30's prev points to 10. The list becomes 10 <-> 30 <-> null.
🔧 Debug
advancedIdentify the Bug in Node Insertion at Head
What error will occur when running this code that inserts a node at the head of a doubly linked list?
DSA C#
public class Node { public int Value; public Node Prev; public Node Next; public Node(int value) { Value = value; Prev = null; Next = null; } } public class DoublyLinkedList { public Node Head; public Node Tail; public void AddFirst(int value) { Node newNode = new Node(value); newNode.Next = Head; if (Head != null) Head.Prev = newNode; Head = newNode; if (Tail == null) Tail = newNode; } } var list = new DoublyLinkedList(); list.AddFirst(10);
Attempts:
2 left
💡 Hint
Check what happens when the list is empty and you access Head.Prev.
✗ Incorrect
When the list is empty, Head is null. Accessing Head.Prev causes a NullReferenceException.
🧠 Conceptual
advancedUnderstanding Node Links in Doubly Linked List
In a doubly linked list, what is the correct relationship between a node and its neighbors?
Attempts:
2 left
💡 Hint
Think about moving forward and backward in the list.
✗ Incorrect
In a doubly linked list, Next points forward, Prev points backward.
🚀 Application
expertResult of Reversing a Doubly Linked List
After reversing a doubly linked list with nodes 1 <-> 2 <-> 3 <-> null, what is the printed state of the list?
DSA C#
public class Node { public int Value; public Node Prev; public Node Next; public Node(int value) { Value = value; Prev = null; Next = null; } } public class DoublyLinkedList { public Node Head; public Node Tail; public void AddLast(int value) { Node newNode = new Node(value); if (Head == null) { Head = Tail = newNode; } else { Tail.Next = newNode; newNode.Prev = Tail; Tail = newNode; } } public void Reverse() { Node current = Head; Node temp = null; while (current != null) { temp = current.Prev; current.Prev = current.Next; current.Next = temp; current = current.Prev; } if (temp != null) { Head = temp.Prev; } } public void PrintList() { Node current = Head; while (current != null) { System.Console.Write(current.Value); if (current.Next != null) System.Console.Write(" <-> "); current = current.Next; } System.Console.WriteLine(" <-> null"); } } var list = new DoublyLinkedList(); list.AddLast(1); list.AddLast(2); list.AddLast(3); list.Reverse(); list.PrintList();
Attempts:
2 left
💡 Hint
Reversing swaps next and prev pointers and updates the head.
✗ Incorrect
Reversing the list changes the order to 3, 2, 1. The print starts from new head 3.
