Challenge - 5 Problems
Doubly Linked List Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Reversing a Doubly Linked List
What is the printed state of the doubly linked list after reversing it using the given method?
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 Reverse() { Node temp = null; Node current = head; 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 node = head; while (node != null) { System.Console.Write(node.data + " <-> "); node = node.next; } System.Console.WriteLine("null"); } } // Setup DoublyLinkedList dll = new DoublyLinkedList(); dll.head = new Node(1); Node second = new Node(2); Node third = new Node(3); // Linking nodes dll.head.next = second; second.prev = dll.head; second.next = third; third.prev = second; // Reverse and print dll.Reverse(); dll.PrintList();
Attempts:
2 left
💡 Hint
Think about how the prev and next pointers are swapped for each node.
✗ Incorrect
The Reverse method swaps the prev and next pointers for each node, then updates the head to the last node processed. This reverses the list order from 1->2->3 to 3->2->1.
🧠 Conceptual
intermediateUnderstanding Pointer Swapping in Reverse
In the reverse method of a doubly linked list, what is the main purpose of swapping the prev and next pointers of each node?
Attempts:
2 left
💡 Hint
Think about what happens to the direction of links when prev and next are swapped.
✗ Incorrect
Swapping prev and next pointers reverses the direction of the links between nodes, effectively reversing the list.
🔧 Debug
advancedIdentify the Bug in Reverse Method
What error will occur if the line 'head = temp.prev;' is replaced with 'head = temp.next;' in the reverse method?
DSA C#
public void Reverse() {
Node temp = null;
Node current = head;
while (current != null) {
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}
if (temp != null) {
head = temp.next; // changed from temp.prev
}
}Attempts:
2 left
💡 Hint
Consider what temp points to at the end of the loop and what temp.next would be.
✗ Incorrect
At the end of the loop, temp points to the previous node of the last processed node. Using temp.next sets head to an intermediate node in the reversed list, resulting in only part of the list being accessible from head and some nodes lost.
❓ Predict Output
advancedOutput After Reversing a Longer Doubly Linked List
Given the doubly linked list 10 <-> 20 <-> 30 <-> 40 <-> null, what is the printed output after reversing it with the standard reverse method?
DSA C#
DoublyLinkedList dll = new DoublyLinkedList(); dll.head = new Node(10); Node n2 = new Node(20); Node n3 = new Node(30); Node n4 = new Node(40); dll.head.next = n2; n2.prev = dll.head; n2.next = n3; n3.prev = n2; n3.next = n4; n4.prev = n3; dll.Reverse(); dll.PrintList();
Attempts:
2 left
💡 Hint
The reverse method swaps prev and next pointers for all nodes.
✗ Incorrect
Reversing swaps the links so the list order is reversed from 10->20->30->40 to 40->30->20->10.
🧠 Conceptual
expertWhy is Updating Head Necessary After Reversing?
After swapping prev and next pointers for all nodes in a doubly linked list, why must we update the head pointer to the last processed node's previous pointer?
Attempts:
2 left
💡 Hint
Think about the position of the head before and after reversal.
✗ Incorrect
After reversal, the original head is at the end (tail), so the head pointer must be updated to the last node processed, which is temp.prev, to correctly point to the new start of the list.
