4. Given the following code snippet for reordering a linked list, what is the main bug causing incorrect output?
void reorderList(ListNode* head) {
if (!head) return;
ListNode* slow = head;
ListNode* fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
ListNode* second = slow;
ListNode* prev = nullptr;
while (second) {
ListNode* tmp = second->next;
second->next = prev;
prev = second;
second = tmp;
}
ListNode* first = head;
second = prev;
while (second) {
ListNode* tmp1 = first->next;
ListNode* tmp2 = second->next;
first->next = second;
second->next = tmp1;
first = tmp1;
second = tmp2;
}
}