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 list after applying the reverse function?
DSA Java
class Node { int data; Node prev, next; Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { Node head; 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; } } void printList() { Node node = head; while (node != null) { System.out.print(node.data + " <-> "); node = node.next; } System.out.println("null"); } } DoublyLinkedList dll = new DoublyLinkedList(); dll.head = new Node(1); Node second = new Node(2); Node third = new Node(3); Node fourth = new Node(4); dll.head.next = second; second.prev = dll.head; second.next = third; third.prev = second; third.next = fourth; fourth.prev = third; dll.reverse(); dll.printList();
Attempts:
2 left
💡 Hint
Think about swapping the prev and next pointers for each node.
✗ Incorrect
The reverse function swaps the prev and next pointers for each node and then updates the head to the last node processed. This reverses the list order.
❓ Predict Output
intermediateWhat is the output of this reverse function on a single-node list?
Given a doubly linked list with only one node (5 <-> null), what is the output after calling reverse() and then printList()?
DSA Java
class Node { int data; Node prev, next; Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { Node head; 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; } } void printList() { Node node = head; while (node != null) { System.out.print(node.data + " <-> "); node = node.next; } System.out.println("null"); } } DoublyLinkedList dll = new DoublyLinkedList(); dll.head = new Node(5); dll.reverse(); dll.printList();
Attempts:
2 left
💡 Hint
Reversing a single node list should not change the list.
✗ Incorrect
Since there is only one node, reversing swaps prev and next which are both null, so the list remains the same.
🔧 Debug
advancedWhy does this reverse function fail to update the head correctly?
Consider this reverse function for a doubly linked list. After reversal, the list prints empty. What is the bug?
DSA Java
void reverse() {
Node temp = null;
Node current = head;
while (current != null) {
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}
head = temp;
}Attempts:
2 left
💡 Hint
Check how head is updated after the loop.
✗ Incorrect
After the loop, temp points to the previous node of the last processed node, so head must be set to temp.prev to point to the new first node.
🧠 Conceptual
advancedWhat is the time complexity of reversing a doubly linked list?
If a doubly linked list has n nodes, what is the time complexity of reversing it by swapping prev and next pointers?
Attempts:
2 left
💡 Hint
Consider how many nodes you visit and update.
✗ Incorrect
Each node is visited once and pointers are swapped, so the operation is linear in the number of nodes.
❓ Predict Output
expertWhat is the output after reversing this doubly linked list with duplicate values?
Given the doubly linked list: 2 <-> 3 <-> 3 <-> 5 <-> null, what is the printed list after applying the reverse function?
DSA Java
class Node { int data; Node prev, next; Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { Node head; 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; } } void printList() { Node node = head; while (node != null) { System.out.print(node.data + " <-> "); node = node.next; } System.out.println("null"); } } DoublyLinkedList dll = new DoublyLinkedList(); dll.head = new Node(2); Node second = new Node(3); Node third = new Node(3); Node fourth = new Node(5); dll.head.next = second; second.prev = dll.head; second.next = third; third.prev = second; third.next = fourth; fourth.prev = third; dll.reverse(); dll.printList();
Attempts:
2 left
💡 Hint
Duplicates do not affect the reversal logic.
✗ Incorrect
The reverse function swaps pointers for all nodes, reversing the order including duplicates.
