← 返回 bytedance 的题目列表Reverse Nodes in k-Group
类型:online_judge
Problem: Reverse Nodes in k-Group
Given the head node head of a singly linked list and an integer k, reverse the nodes of the list k at a time and return the modified list.
If the number of remaining nodes is less than k, keep them in the original order.
You may only change node pointers, not swap node values.
Input Format
The first line contains an integer n, the number of nodes.
The second line contains n integers, representing node values from head to tail. If n = 0, the second line may be empty or absent.
The third line contains an integer k.
Output Format
Print the processed linked list values from head to tail, separated by spaces. If the list is empty, print an empty line.
Constraints
0 <= n <= 5000
1 <= k <= 5000
-5000 <= Node.val <= 5000
Example
Input:
5
1 2 3 4 5
2
Output:
2 1 4 3 5
Example
Input
5
1 2 3 4 5
2
Output
2 1 4 3 5