← 返回 bytedance 的题目列表Reverse Nodes in k-Group
类型:online_judge
Given a singly linked list and an integer (k), reverse the nodes of the list in groups of (k) in place and return the resulting head.
If fewer than (k) nodes remain at the end, leave them unchanged.
You may not modify node values; only relink pointers.
Use (O(1)) auxiliary space, excluding the output list.
Input Format
First line: integers n k, the number of nodes and group size.
Second line: n integers representing node values.
Output Format
Print the resulting list values separated by spaces.
Example
Input:
5 2
1 2 3 4 5
Output:
2 1 4 3 5
Constraints
(0 \le n \le 10^5)
(1 \le k \le 10^5)
Node values are 32-bit signed integers.
Example
Input
5 2
1 2 3 4 5
Output
2 1 4 3 5