A recap screen shows a project's milestones as a linked list L0, L1, ..., Ln. To contrast early and late work, it wants them interleaved from both ends: L0, Ln, L1, Ln-1, L2, Ln-2, ....
Write foldTimeline(head) that relinks the nodes into that order and returns the head. Don't change any val; move the nodes. Use O(1) extra space.
Input: head = [1,2,3,4] Output: [1,4,2,3]
First 1, then the last 4, then 2, then 3.
Input: head = [1,2,3,4,5] Output: [1,5,2,4,3]
1, 5, 2, 4, and the middle 3 ends the list.
0 to 5 * 10^4 nodes.1 <= node.val <= 1000Lists are written as arrays here, like [1, 2, 3]. Your function works with ListNode objects, each with val and next.
Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #233 Fold a Timeline. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.