← 返回 bytedance 的题目列表Reverse Nodes in k-Group
类型:online_judge
Reverse Nodes in k-Group
Given a linked list, reverse the nodes of the list k at a time and return its modified list.
You may not alter the values in the nodes, only nodes themselves may be changed.
Only constant memory is allowed.
Example:
Input: 1->2->3->4->5 with k = 2
Output: 2->1->4->3->5
Note:
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
Complexity allowed: O(n)
Constraints:
The number of nodes in the list is in the range [0, 5000].
1 <= k <= n
Example
Input
1->2->3->4->5; k = 2