Challenge - 5 Problems
Linked List Reversal Master
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 is the printed state after running the iterative reverse function?
DSA Typescript
class ListNode { val: number; next: ListNode | null; constructor(val: number, next: ListNode | null = null) { this.val = val; this.next = next; } } function reverseList(head: ListNode | null): ListNode | null { let prev: ListNode | null = null; let current = head; while (current !== null) { const nextTemp = current.next; current.next = prev; prev = current; current = nextTemp; } return prev; } // Create linked list 1 -> 2 -> 3 -> null const head = new ListNode(1, new ListNode(2, new ListNode(3))); const reversedHead = reverseList(head); // Function to print list function printList(node: ListNode | null): string { let result = ''; while (node !== null) { result += node.val + ' -> '; node = node.next; } return result + 'null'; } console.log(printList(reversedHead));
Attempts:
2 left
💡 Hint
Think about how the pointers change direction step by step.
✗ Incorrect
The iterative reverse function changes the direction of the links one by one. Starting from 1 -> 2 -> 3 -> null, after reversal, the list becomes 3 -> 2 -> 1 -> null.
❓ Predict Output
intermediateWhat is the output after reversing a single-node linked list?
Given a linked list with a single node 5 -> null, what is the printed state after running the iterative reverse function?
DSA Typescript
class ListNode { val: number; next: ListNode | null; constructor(val: number, next: ListNode | null = null) { this.val = val; this.next = next; } } function reverseList(head: ListNode | null): ListNode | null { let prev: ListNode | null = null; let current = head; while (current !== null) { const nextTemp = current.next; current.next = prev; prev = current; current = nextTemp; } return prev; } const head = new ListNode(5); const reversedHead = reverseList(head); function printList(node: ListNode | null): string { let result = ''; while (node !== null) { result += node.val + ' -> '; node = node.next; } return result + 'null'; } console.log(printList(reversedHead));
Attempts:
2 left
💡 Hint
Reversing a single node list should not change the list.
✗ Incorrect
A single node linked list reversed remains the same because there is no other node to change the direction of links.
🔧 Debug
advancedIdentify the error in this iterative reverse function
What error will this code produce when reversing a linked list?
DSA Typescript
function reverseList(head: ListNode | null): ListNode | null {
let prev: ListNode | null = null;
let current = head;
while (current !== null) {
const nextTemp = current.next;
current.next = current;
prev = current;
current = nextTemp;
}
return prev;
}Attempts:
2 left
💡 Hint
Look at how the next pointer is assigned inside the loop.
✗ Incorrect
The line current.next = current; makes the current node point to itself, causing an infinite loop when traversing.
❓ Predict Output
advancedWhat is the output after reversing an empty linked list?
Given an empty linked list (head is null), what is the printed state after running the iterative reverse function?
DSA Typescript
class ListNode { val: number; next: ListNode | null; constructor(val: number, next: ListNode | null = null) { this.val = val; this.next = next; } } function reverseList(head: ListNode | null): ListNode | null { let prev: ListNode | null = null; let current = head; while (current !== null) { const nextTemp = current.next; current.next = prev; prev = current; current = nextTemp; } return prev; } const head = null; const reversedHead = reverseList(head); function printList(node: ListNode | null): string { let result = ''; while (node !== null) { result += node.val + ' -> '; node = node.next; } return result + 'null'; } console.log(printList(reversedHead));
Attempts:
2 left
💡 Hint
Reversing an empty list should return empty.
✗ Incorrect
Since the input list is empty, the reversed list is also empty, so printing shows just 'null'.
❓ Predict Output
expertWhat is the output after reversing this linked list with duplicate values?
Given the linked list 4 -> 4 -> 5 -> 6 -> null, what is the printed state after running the iterative reverse function?
DSA Typescript
class ListNode { val: number; next: ListNode | null; constructor(val: number, next: ListNode | null = null) { this.val = val; this.next = next; } } function reverseList(head: ListNode | null): ListNode | null { let prev: ListNode | null = null; let current = head; while (current !== null) { const nextTemp = current.next; current.next = prev; prev = current; current = nextTemp; } return prev; } const head = new ListNode(4, new ListNode(4, new ListNode(5, new ListNode(6)))); const reversedHead = reverseList(head); function printList(node: ListNode | null): string { let result = ''; while (node !== null) { result += node.val + ' -> '; node = node.next; } return result + 'null'; } console.log(printList(reversedHead));
Attempts:
2 left
💡 Hint
The reversal should keep all nodes but reverse their order.
✗ Incorrect
The iterative reversal reverses the entire list including duplicates, so the order becomes 6 -> 5 -> 4 -> 4 -> null.
