Challenge - 5 Problems
Master of Linked List Reversal
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of reversing this linked list iteratively?
Given the linked list 1 -> 2 -> 3 -> null, what will be the printed state after running the iterative reverse function?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } function reverseList(head) { let prev = null; let current = head; while (current !== null) { let nextTemp = current.next; current.next = prev; prev = current; current = nextTemp; } return prev; } // Create linked list 1 -> 2 -> 3 -> null const head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); const reversedHead = reverseList(head); // Function to print list function printList(node) { let result = ''; while (node !== null) { result += node.value + ' -> '; node = node.next; } result += 'null'; console.log(result); } printList(reversedHead);
Attempts:
2 left
💡 Hint
Think about how the pointers change direction in each iteration.
✗ Incorrect
The iterative reverse function changes the next pointer of each node to point to the previous node, effectively reversing the list. Starting from 1 -> 2 -> 3 -> null, after reversal it becomes 3 -> 2 -> 1 -> null.
❓ Predict Output
intermediateWhat is the output after reversing a single-node linked list?
Given a linked list with only one node: 5 -> null, what will be the printed state after running the iterative reverse function?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } function reverseList(head) { let prev = null; let current = head; while (current !== null) { let nextTemp = current.next; current.next = prev; prev = current; current = nextTemp; } return prev; } const head = new Node(5); const reversedHead = reverseList(head); function printList(node) { let result = ''; while (node !== null) { result += node.value + ' -> '; node = node.next; } result += 'null'; console.log(result); } printList(reversedHead);
Attempts:
2 left
💡 Hint
Reversing a single node list should not change the list.
✗ Incorrect
A single node pointing to null reversed still points to null, so the list remains the same.
❓ Predict Output
advancedWhat is the output after reversing this longer linked list iteratively?
Given the linked list 10 -> 20 -> 30 -> 40 -> null, what will be the printed state after running the iterative reverse function?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } function reverseList(head) { let prev = null; let current = head; while (current !== null) { let nextTemp = current.next; current.next = prev; prev = current; current = nextTemp; } return prev; } const head = new Node(10); head.next = new Node(20); head.next.next = new Node(30); head.next.next.next = new Node(40); const reversedHead = reverseList(head); function printList(node) { let result = ''; while (node !== null) { result += node.value + ' -> '; node = node.next; } result += 'null'; console.log(result); } printList(reversedHead);
Attempts:
2 left
💡 Hint
The last node becomes the new head after reversal.
✗ Incorrect
The iterative reversal changes the direction of all next pointers, so the list is reversed completely.
❓ Predict Output
advancedWhat is the output after reversing an empty linked list?
Given an empty linked list (head is null), what will be the printed state after running the iterative reverse function?
DSA Javascript
function reverseList(head) {
let prev = null;
let current = head;
while (current !== null) {
let nextTemp = current.next;
current.next = prev;
prev = current;
current = nextTemp;
}
return prev;
}
const head = null;
const reversedHead = reverseList(head);
function printList(node) {
let result = '';
while (node !== null) {
result += node.value + ' -> ';
node = node.next;
}
result += 'null';
console.log(result);
}
printList(reversedHead);Attempts:
2 left
💡 Hint
Reversing an empty list should return empty.
✗ Incorrect
Since the list is empty, reversing it returns null, so the printed output is just 'null'.
🧠 Conceptual
expertWhich statement about iterative reversal of singly linked lists is true?
Consider the iterative method to reverse a singly linked list. Which of the following statements is correct?
Attempts:
2 left
💡 Hint
Think about how pointers are changed during iteration.
✗ Incorrect
Iterative reversal changes each node's next pointer to the previous node, using only a few variables, so it uses constant extra space.
