#C1645. Reverse Nodes in k-Group

    ID: 44873 Type: Default 1000ms 256MiB

Reverse Nodes in k-Group

Reverse Nodes in k-Group

Given a singly linked list, reverse the nodes of the list in groups of size kk, and return the modified list. If the number of nodes is not a multiple of kk, then the remaining nodes should remain in their original order.

For example, for an input linked list (L = [1,2,3,4,5,6,7,8]) and (k = 3), the list after processing would be ([3,2,1,6,5,4,7,8]).

inputFormat

The first line contains an integer (n) (the number of nodes). The second line contains (n) space-separated integers representing the node values of the linked list. The third line contains an integer (k) which indicates the group size for reversal.

outputFormat

Output a single line containing the values of the modified linked list after reversing every group of (k) nodes. The node values should be printed in order separated by a space.## sample

8
1 2 3 4 5 6 7 8
3
3 2 1 6 5 4 7 8