← 返回 bytedance 的题目列表Reverse the Order of k-Groups in a Linked List
类型:online_judge
Given the head of a singly linked list and an integer k, split the list from left to right into groups of size k.
Unlike LeetCode 25, do not reverse the nodes inside each group. Instead, reverse the order of the complete k-groups, while preserving the internal order of nodes within every group.
If the last group contains fewer than k nodes, it is not considered a complete group and should remain at the end of the result list.
Input Format
n k
v1 v2 ... vn
n is the number of nodes in the linked list.
The second line contains the node values.
Output Format
Print the transformed linked list values separated by spaces.
Example
Input:
8 3
1 2 3 4 5 6 7 8
The complete k-groups are [1,2,3] and [4,5,6]; the remaining [7,8] is incomplete.
Output:
4 5 6 1 2 3 7 8
Constraints
0 <= n <= 10^5
1 <= k <= 10^5
Node values are 32-bit integers.
Example
Input
8 3
1 2 3 4 5 6 7 8
Output
4 5 6 1 2 3 7 8