Description

Given the head of a singly linked list, reorder it to: L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → ... You may not modify the values in the list's nodes.

Examples

Input:head = [1,2,3,4]
Output:[1,4,2,3]
Explanation:

Reordered as specified.

Input:head = [10]
Output:[10]
Explanation:

With only one node, there's no reordering possible. The single node remains as is.

Input:head = [7,8,9,10,11,12]
Output:[7,12,8,11,9,10]
Explanation:

For a 6-node list, alternating between nodes from start and end: first node (7), last node (12), second node (8), second-to-last node (11), third node (9), and third-to-last node (10).

Constraints

  • Number of nodes is in range [1, 5 × 10⁴]
  • 1 ≤ Node.val ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!