Reorder a Linked List
Implement reorderList
Given the head of a singly linked list L0 → L1 → ... → Ln-1 → Ln, reorder it in place into L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → ..., without changing any node's value.
The optimal solution turns this into a problem it already knows how to solve twice over: find the middle and split the list into two halves, reverse the second half (the same relink primitive used to reverse any run of nodes), and then merge the two halves — one running forward from the front, one running "backward" from the end — by strictly alternating a node from each side. That alternation is exactly what produces the L0, Ln, L1, Ln-1, ... pattern, with every step just re-linking existing nodes.
Example 1:
Input: head = [1,2,3,4]
Output: [1,4,2,3]
Example 2:
Input: head = [1,2,3,4,5]
Output: [1,5,2,4,3]
Example 3:
Input: head = [1]
Output: [1]
+ 5 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ number of nodes in head ≤ 200 - ●
-1000 ≤ node value ≤ 1000
head =
[1, 2, 3, 4]