I spent way too long second-guessing the final group edge case.
Use a dummy node to simplify edge cases, then iterate through the list in groups of increasing size. For each group, determine its actual length, and if even, reverse it in place; otherwise, leave it unchanged. Carefully link the previous group's tail to the current group's head after processing.
Pro tip: Emphasize the importance of handling the last group correctly by checking its actual length before deciding to reverse, and discuss how the dummy node technique simplifies pointer manipulation and edge cases.
Create a dummy node pointing to the head to simplify edge cases. Maintain a pointer to the last node of the previously processed group (prev_tail) and a pointer to the current node (curr).
Use a variable group_size starting at 1. For each group, traverse group_size nodes from curr to find the group's end and count the actual number of nodes (actual_len).
If actual_len is even, reverse the group by adjusting pointers; otherwise, leave it as is. Ensure the reversed group's tail connects to the next node.
Connect the previous group's tail to the current group's head (after reversal if applied). Update prev_tail to the last node of the current group and move curr to the next node.
Increment group_size and repeat steps 2-4 until curr becomes null. Return dummy.next as the new head.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.