Difficult:Hard
題目
Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list.
k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is.
You may not alter the values in the list’s nodes, only nodes themselves may be changed.
翻譯
給定鍊錶的頭部,一次反轉鍊錶 k 的節點,並返回修改後的鍊錶。
k 為正整數,小於等於鍊錶的長度。如果節點的數量不是 k 的倍數,那麼最後剩下的節點應該保持原樣。
您不能更改列表節點中的值,只能更改節點本身。
範例
Example 1
1 | Input: head = [1,2,3,4,5], k = 2 |
Example 2
1 | Input: head = [1,2,3,4,5], k = 3 |
解題思路
Solution
Code 1 :
1 | var reverseKGroup = function(head, k) { |