You are given a singly linked list. Rearrange it in a new order:
First node → Last node → Second node → Second last node → Third node → …
You cannot change the node values; only the node links can be changed.
Input: head = [10,20,30,40]
Output: [10,40,20,30]
Explanation:
Rearrange as first → last → second → second last.
Input: head = [1,2,3,4,5]
Output: [1,5,2,4,3]
Input: head = [7,8,9]
Output: [7,9,8]
Accepted:
Submission: