Challenge - 5 Problems
Reorder List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Reordering a Linked List
What is the printed state of the linked list after running the reorder function on the list 1 -> 2 -> 3 -> 4 -> 5 -> null?
DSA Javascript
class ListNode { constructor(val = 0, next = null) { this.val = val; this.next = next; } } function printList(head) { let result = []; while (head) { result.push(head.val); head = head.next; } return result.join(' -> ') + ' -> null'; } function reorderList(head) { if (!head || !head.next) return; // Find middle let slow = head, fast = head; while (fast.next && fast.next.next) { slow = slow.next; fast = fast.next.next; } // Reverse second half let prev = null, curr = slow.next; while (curr) { let nextTemp = curr.next; curr.next = prev; prev = curr; curr = nextTemp; } slow.next = null; // Merge two halves let first = head, second = prev; while (second) { let temp1 = first.next; let temp2 = second.next; first.next = second; second.next = temp1; first = temp1; second = temp2; } } // Setup linked list let head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5))))); reorderList(head); console.log(printList(head));
Attempts:
2 left
💡 Hint
Think about splitting the list into two halves, reversing the second half, then merging alternately.
✗ Incorrect
The reorderList function splits the list into two halves, reverses the second half, and then merges nodes alternately from the first and reversed second half. This results in the order 1 -> 5 -> 2 -> 4 -> 3 -> null.
🧠 Conceptual
intermediateUnderstanding the Reorder List Algorithm
Which step is NOT part of the standard approach to reorder a linked list as per the common algorithm?
Attempts:
2 left
💡 Hint
Reordering does not involve sorting the list.
✗ Incorrect
The reorder list algorithm involves finding the middle, reversing the second half, and merging alternately. Sorting the list is not part of this process.
🔧 Debug
advancedIdentify the Error in Reorder List Implementation
What error will this code produce when trying to reorder the list 1 -> 2 -> 3 -> 4 -> null?
function reorderList(head) {
if (!head) return;
let slow = head, fast = head.next;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
}
let second = slow.next;
slow.next = null;
let prev = null;
while (second) {
let temp = second.next;
second.next = prev;
prev = second;
second = temp;
}
let first = head;
while (prev) {
let temp1 = first.next;
let temp2 = prev.next;
first.next = prev;
prev.next = temp1;
first = temp1;
prev = temp2;
}
}
Attempts:
2 left
💡 Hint
Check how the pointers move and if any can become null unexpectedly.
✗ Incorrect
The code sets fast = head.next initially, which causes the while loop to run incorrectly for even length lists, leading to first or prev becoming null and accessing next property causing TypeError.
📝 Syntax
advancedSyntax Error in Reorder List Code
Which option contains a syntax error that will prevent the reorder list function from running?
DSA Javascript
function reorderList(head) {
if (!head || !head.next) return;
let slow = head, fast = head;
while (fast.next && fast.next.next) {
slow = slow.next;
fast = fast.next.next;
}
let second = slow.next;
slow.next = null;
let prev = null;
while (second) {
let temp = second.next;
second.next = prev;
prev = second;
second = temp;
}
let first = head;
while (prev) {
let temp1 = first.next;
let temp2 = prev.next;
first.next = prev;
prev.next = temp1;
first = temp1;
prev = temp2;
}
}Attempts:
2 left
💡 Hint
Look for missing closing braces or parentheses.
✗ Incorrect
Option A is missing the closing brace for the function, causing a syntax error.
🚀 Application
expertReorder List with Even Number of Nodes
Given the linked list 10 -> 20 -> 30 -> 40 -> 50 -> 60 -> null, what is the state of the list after applying the reorderList function?
DSA Javascript
class ListNode { constructor(val = 0, next = null) { this.val = val; this.next = next; } } function reorderList(head) { if (!head || !head.next) return; let slow = head, fast = head; while (fast.next && fast.next.next) { slow = slow.next; fast = fast.next.next; } let second = slow.next; slow.next = null; let prev = null; while (second) { let temp = second.next; second.next = prev; prev = second; second = temp; } let first = head; while (prev) { let temp1 = first.next; let temp2 = prev.next; first.next = prev; prev.next = temp1; first = temp1; prev = temp2; } } // Setup linked list let head = new ListNode(10, new ListNode(20, new ListNode(30, new ListNode(40, new ListNode(50, new ListNode(60)))))); reorderList(head); function printList(head) { let result = []; while (head) { result.push(head.val); head = head.next; } return result.join(' -> ') + ' -> null'; } console.log(printList(head));
Attempts:
2 left
💡 Hint
Remember to split, reverse second half, then merge alternately.
✗ Incorrect
The reorderList function splits the list at the middle (after 30), reverses the second half (60 -> 50 -> 40), then merges alternately with the first half (10 -> 20 -> 30), resulting in 10 -> 60 -> 20 -> 50 -> 30 -> 40 -> null.
