Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.
The first node is considered odd, and the second node is even, and so on.
Note that the relative order inside both the even and odd groups should remain as it was in the input.
You must solve the problem in O(1) extra space complexity and O(n) time complexity.
Constraints:
The number of nodes in the linked list is in the range [0, 10^4].
-10^6 <= Node.val <= 10^6
給予一個單向串列陣列的開頭,將全部索引為奇數的節點當作一組,然後偶數節點緊跟在後,並回傳重新排序過後的結果。
第一個節點被視為奇數,第二個為偶數,以此類推。
注意奇數與偶數組內相對的順序應該保持與輸入時相同。
你應該在 O(1) 的額外空間複雜度與 O(n) 的時間複雜度中完成這道題目。
限制:
節點的總數在 [0, 10^4] 之中
-10^6 <= 節點的值 <= 10^6
LeetCode
C++