Challenge - 5 Problems
Reorder List Mastery
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 reorderList method on the list 1 -> 2 -> 3 -> 4 -> 5 -> null?
DSA Java
class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } public class Solution { public void reorderList(ListNode head) { if (head == null || head.next == null) return; // Find middle ListNode slow = head, fast = head; while (fast.next != null && fast.next.next != null) { slow = slow.next; fast = fast.next.next; } // Reverse second half ListNode prev = null, curr = slow.next; while (curr != null) { ListNode nextTemp = curr.next; curr.next = prev; prev = curr; curr = nextTemp; } slow.next = null; // Merge two halves ListNode first = head, second = prev; while (second != null) { ListNode tmp1 = first.next; ListNode tmp2 = second.next; first.next = second; second.next = tmp1; first = tmp1; second = tmp2; } } public static void printList(ListNode head) { ListNode curr = head; while (curr != null) { System.out.print(curr.val); if (curr.next != null) System.out.print(" -> "); curr = curr.next; } System.out.println(" -> null"); } public static void main(String[] args) { ListNode head = new ListNode(1); head.next = new ListNode(2); head.next.next = new ListNode(3); head.next.next.next = new ListNode(4); head.next.next.next.next = new ListNode(5); Solution sol = new Solution(); sol.reorderList(head); printList(head); } }
Attempts:
2 left
💡 Hint
Think about splitting the list into two halves, reversing the second half, then merging alternately.
✗ Incorrect
The reorderList method splits the list into two halves, reverses the second half, and then merges nodes alternately from the first and reversed second half. For the input 1->2->3->4->5, the reordered list is 1->5->2->4->3->null.
🔧 Debug
intermediateIdentify the error in this reorderList implementation
What error will this code produce when reorderList is called on a list with nodes 1 -> 2 -> 3 -> null?
DSA Java
public void reorderList(ListNode head) {
if (head == null || head.next == null) return;
ListNode slow = head, fast = head.next;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode second = slow.next;
slow.next = null;
ListNode prev = null;
while (second != null) {
ListNode next = second.next;
second.next = prev;
prev = second;
second = next;
}
ListNode first = head;
while (prev != null) {
ListNode tmp1 = first.next;
ListNode tmp2 = prev.next;
first.next = prev;
prev.next = tmp1;
first = tmp1;
prev = tmp2;
}
}Attempts:
2 left
💡 Hint
Check what happens when first or prev becomes null inside the merge loop.
✗ Incorrect
The code can cause NullPointerException because in the merge loop, first can become null before prev, and accessing first.next causes the exception.
🚀 Application
advancedReordering a Linked List with Even Number of Nodes
Given the linked list 10 -> 20 -> 30 -> 40 -> null, what is the state of the list after applying the reorderList method?
Attempts:
2 left
💡 Hint
Remember to split, reverse second half, then merge alternately.
✗ Incorrect
For even nodes, the list is split into two halves (10->20 and 30->40), second half reversed (40->30), then merged alternately to get 10->40->20->30->null.
📝 Syntax
advancedIdentify the syntax error in this reorderList snippet
Which option contains the syntax error when trying to reverse the second half of the linked list?
DSA Java
ListNode prev = null, curr = slow.next;
while (curr != null) {
ListNode nextTemp = curr.next
curr.next = prev;
prev = curr;
curr = nextTemp;
}Attempts:
2 left
💡 Hint
Check each line carefully for punctuation errors.
✗ Incorrect
The line 'ListNode nextTemp = curr.next' is missing a semicolon at the end, causing a syntax error.
🧠 Conceptual
expertUnderstanding the Time Complexity of Reorder List
What is the overall time complexity of the reorderList algorithm that splits the list, reverses the second half, and merges the two halves?
Attempts:
2 left
💡 Hint
Consider how many times each node is visited during the process.
✗ Incorrect
Each step (finding middle, reversing, merging) takes linear time, so overall complexity is O(n).
