Challenge - 5 Problems
Doubly Linked List Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after reversing this doubly linked list?
Given the doubly linked list 1 <-> 2 <-> 3 <-> 4 <-> null, what is the printed state after reversing it?
DSA Typescript
class Node { value: number; next: Node | null; prev: Node | null; constructor(value: number) { this.value = value; this.next = null; this.prev = null; } } class DoublyLinkedList { head: Node | null; tail: Node | null; constructor() { this.head = null; this.tail = null; } append(value: number) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; return; } this.tail!.next = newNode; newNode.prev = this.tail; this.tail = newNode; } reverse() { let current = this.head; let temp = null; while (current) { temp = current.prev; current.prev = current.next; current.next = temp; current = current.prev; } if (temp) { this.head = temp.prev; } } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' <-> '; current = current.next; } result += 'null'; console.log(result); } } const dll = new DoublyLinkedList(); dll.append(1); dll.append(2); dll.append(3); dll.append(4); dll.reverse(); dll.printList();
Attempts:
2 left
💡 Hint
Think about how the next and prev pointers swap during reversal.
✗ Incorrect
Reversing a doubly linked list swaps the next and prev pointers of each node. After reversal, the list order is reversed, so the head becomes the tail and vice versa.
🧠 Conceptual
intermediateWhich pointer changes are essential to reverse a doubly linked list?
To reverse a doubly linked list, which pointers must be swapped for each node?
Attempts:
2 left
💡 Hint
Remember each node has two pointers: next and prev.
✗ Incorrect
Reversing requires swapping both next and prev pointers for each node to reverse the direction of the list.
🔧 Debug
advancedWhat error occurs when running this reverse function on a doubly linked list?
Identify the error in the reverse method below and its effect.
DSA Typescript
reverse() {
let current = this.head;
while (current) {
let temp = current.next;
current.next = current.prev;
current.prev = temp;
current = current.next;
}
this.head = current;
}Attempts:
2 left
💡 Hint
Check how current is updated and what happens to head at the end.
✗ Incorrect
The head is set to current after the loop, but current is null at loop end, so head becomes null and list appears empty.
❓ Predict Output
advancedWhat is the output of this reversed doubly linked list print?
After reversing the list 10 <-> 20 <-> 30, what does the printList method output?
DSA Typescript
class Node { value: number; next: Node | null; prev: Node | null; constructor(value: number) { this.value = value; this.next = null; this.prev = null; } } class DoublyLinkedList { head: Node | null; tail: Node | null; constructor() { this.head = null; this.tail = null; } append(value: number) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; return; } this.tail!.next = newNode; newNode.prev = this.tail; this.tail = newNode; } reverse() { let current = this.head; let temp = null; while (current) { temp = current.prev; current.prev = current.next; current.next = temp; current = current.prev; } if (temp) { this.head = temp.prev; } } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' <-> '; current = current.next; } result += 'null'; console.log(result); } } const dll = new DoublyLinkedList(); dll.append(10); dll.append(20); dll.append(30); dll.reverse(); dll.printList();
Attempts:
2 left
💡 Hint
Trace the pointer swaps carefully for each node.
✗ Incorrect
Reversing swaps next and prev pointers, so the list order reverses from 10->20->30 to 30->20->10.
🧠 Conceptual
expertWhy is it necessary to update the head pointer after reversing a doubly linked list?
After swapping next and prev pointers for all nodes, why must the head pointer be updated?
Attempts:
2 left
💡 Hint
Think about what happens to the first node after reversal.
✗ Incorrect
Reversing swaps directions, so the original head is now the last node. The head pointer must be updated to the new first node to maintain list integrity.
