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 with 3 nodes
Given a doubly linked list with nodes 1 <-> 2 <-> 3, what is the printed state after reversing it?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; } append(data) { const newNode = new Node(data); if (!this.head) { this.head = newNode; return; } let curr = this.head; while (curr.next) { curr = curr.next; } curr.next = newNode; newNode.prev = curr; } reverse() { let curr = this.head; let temp = null; while (curr) { temp = curr.prev; curr.prev = curr.next; curr.next = temp; curr = curr.prev; } if (temp) { this.head = temp.prev; } } printList() { let curr = this.head; let result = ''; while (curr) { result += curr.data + ' -> '; curr = curr.next; } result += 'null'; console.log(result); } } const dll = new DoublyLinkedList(); dll.append(1); dll.append(2); dll.append(3); dll.reverse(); dll.printList();
Attempts:
2 left
💡 Hint
Think about how reversing swaps next and prev pointers.
✗ Incorrect
Reversing swaps the next and prev pointers of each node. The head moves to the last node, so the list prints 3 -> 2 -> 1 -> null.
❓ Predict Output
intermediateOutput after reversing an empty doubly linked list
What is the output when reversing and printing an empty doubly linked list?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; } reverse() { let curr = this.head; let temp = null; while (curr) { temp = curr.prev; curr.prev = curr.next; curr.next = temp; curr = curr.prev; } if (temp) { this.head = temp.prev; } } printList() { let curr = this.head; let result = ''; while (curr) { result += curr.data + ' -> '; curr = curr.next; } result += 'null'; console.log(result); } } const dll = new DoublyLinkedList(); dll.reverse(); dll.printList();
Attempts:
2 left
💡 Hint
Check what happens when head is null.
✗ Incorrect
Since the list is empty, head is null. The reverse method does nothing and printList prints 'null'.
🔧 Debug
advancedIdentify the bug in this reverse method for doubly linked list
What is the bug in this reverse method that causes incorrect reversal?
DSA Javascript
reverse() {
let curr = this.head;
let temp = null;
while (curr) {
temp = curr.next;
curr.next = curr.prev;
curr.prev = temp;
curr = curr.next;
}
if (temp) {
this.head = temp.prev;
}
}Attempts:
2 left
💡 Hint
Check what temp stores before swapping pointers.
✗ Incorrect
The code stores curr.next in temp but should store curr.prev to swap pointers correctly. This causes incorrect reversal.
❓ Predict Output
advancedOutput after reversing a doubly linked list with one node
What is the printed output after reversing a doubly linked list with a single node containing 5?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; } append(data) { const newNode = new Node(data); if (!this.head) { this.head = newNode; return; } let curr = this.head; while (curr.next) { curr = curr.next; } curr.next = newNode; newNode.prev = curr; } reverse() { let curr = this.head; let temp = null; while (curr) { temp = curr.prev; curr.prev = curr.next; curr.next = temp; curr = curr.prev; } if (temp) { this.head = temp.prev; } } printList() { let curr = this.head; let result = ''; while (curr) { result += curr.data + ' -> '; curr = curr.next; } result += 'null'; console.log(result); } } const dll = new DoublyLinkedList(); dll.append(5); dll.reverse(); dll.printList();
Attempts:
2 left
💡 Hint
Reversing a single node list should not change the list.
✗ Incorrect
A single node reversed remains the same. The output prints '5 -> null'.
🧠 Conceptual
expertWhy does reversing a doubly linked list require swapping next and prev pointers?
Choose the best explanation for why reversing a doubly linked list involves swapping the next and prev pointers of each node.
Attempts:
2 left
💡 Hint
Think about how traversal direction changes after reversal.
✗ Incorrect
Swapping next and prev pointers reverses the links between nodes, so traversal direction reverses and the last node becomes the new head.
