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 C++
struct Node {
int val;
Node* next;
Node(int x) : val(x), next(nullptr) {}
};
void reorderList(Node* head) {
if (!head || !head->next) return;
Node* slow = head;
Node* fast = head;
while (fast->next && fast->next->next) {
slow = slow->next;
fast = fast->next->next;
}
Node* second = slow->next;
slow->next = nullptr;
Node* prev = nullptr;
while (second) {
Node* tmp = second->next;
second->next = prev;
prev = second;
second = tmp;
}
Node* first = head;
second = prev;
while (second) {
Node* tmp1 = first->next;
Node* tmp2 = second->next;
first->next = second;
second->next = tmp1;
first = tmp1;
second = tmp2;
}
}
void printList(Node* head) {
while (head) {
std::cout << head->val;
if (head->next) std::cout << " -> ";
head = head->next;
}
std::cout << " -> null" << std::endl;
}
int main() {
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
head->next->next->next->next = new Node(5);
reorderList(head);
printList(head);
return 0;
}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 at the middle, reverses the second half, then merges nodes alternately from the first and reversed second half. For the list 1->2->3->4->5, the reordered list is 1->5->2->4->3.
🧠 Conceptual
intermediateUnderstanding the Reorder Linked 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 linked list algorithm involves finding the middle, reversing the second half, and merging alternately. Sorting the list is not part of this algorithm.
🔧 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?
Code snippet:
void reorderList(Node* head) {
if (!head) return;
Node* slow = head;
Node* fast = head->next;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
Node* second = slow->next;
slow->next = nullptr;
Node* prev = nullptr;
while (second) {
Node* tmp = second->next;
second->next = prev;
prev = second;
second = tmp;
}
Node* first = head;
second = prev;
while (second) {
Node* tmp1 = first->next;
Node* tmp2 = second->next;
first->next = second;
second->next = tmp1;
first = tmp1;
second = tmp2;
}
}
Attempts:
2 left
💡 Hint
Check how fast pointer is initialized and used in the while loop.
✗ Incorrect
Initializing fast to head->next causes the while loop to skip the middle node incorrectly for even length lists, leading to incorrect splitting and possible null pointer dereference during merging.
📝 Syntax
advancedCorrect the Syntax Error in Reorder List Code
Which option fixes the syntax error in this snippet?
Code snippet:
Node* prev = nullptr
while (second) {
Node* tmp = second->next;
second->next = prev;
prev = second;
second = tmp;
}
Attempts:
2 left
💡 Hint
Check the line before the while loop for missing punctuation.
✗ Incorrect
The line 'Node* prev = nullptr' is missing a semicolon, causing a syntax error. Adding the semicolon fixes the syntax error.
🚀 Application
expertFinal Order of Nodes After Reordering
Given the linked list 10 -> 20 -> 30 -> 40 -> 50 -> 60 -> null, what is the order of nodes after applying the reorder list algorithm?
Attempts:
2 left
💡 Hint
Split the list in half, reverse the second half, then merge alternately.
✗ Incorrect
The list splits into 10->20->30 and 40->50->60. Reversing second half gives 60->50->40. Merging alternately results in 10->60->20->50->30->40.
