I knew the general idea pretty fast: keep two pointers, one crawling odd nodes and one crawling even, then stitch the even chain onto the end of the odd chain.
Use two pointers to separate odd and even indexed nodes into two sublists while traversing the original list once. Then connect the end of the odd list to the head of the even list. This achieves O(n) time and O(1) space by rearranging pointers in place.
Pro tip: Clarify whether indices are 1-based or 0-based, as this affects which nodes are considered odd/even. Also, handle edge cases like empty list, single node, or two nodes to avoid null pointer exceptions.
Confirm indexing convention (1-based vs 0-based) and discuss edge cases such as empty list, single node, or two nodes. This ensures correct interpretation and robust handling.
Create pointers for odd and even lists: oddHead, oddTail, evenHead, evenTail. Start with the first node as odd and second as even, if they exist.
Iterate through the list, linking odd nodes to the odd list and even nodes to the even list, updating tails accordingly. Advance by two nodes each step.
After traversal, connect the tail of the odd list to the head of the even list. Ensure the last node of the even list points to null to terminate the list.
Return the head of the odd list (or even list if odd is empty). Walk through an example to verify correctness and edge cases.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.