Interleave the two ends of a list
Write fold_list(head): reorder a singly linked list in place so it alternates between its front and its back: first node, last node, second node, second-to-last node, and so on. 1 → 2 → 3 → 4 → 5 becomes 1 → 5 → 2 → 4 → 3. The first node stays first, so return nothing. The list may be empty (head is None). Each ListNode has val and next.
Move the nodes instead of rewriting their values, in O(n) time and O(1) extra memory (no list of nodes).
The tests call the provided run_fold_list(values), which builds the list from values, calls your function and returns the values from the original first node onwards.
Besides the tests, one large input (a 10,000-node list) with a ~180 ms limit (scaled to your device's speed): brute force won't pass.
Examples
run_fold_list([1,2,3,4,5])→[1,5,2,4,3]run_fold_list([1,2,3,4])→[1,4,2,3]run_fold_list([7])→[7]