The elephant and its friends are still queuing in a line. However, they are all facing the wrong direction, so they decide to reverse their direction in groups.
Since the elephant and its friends are too lazy to call each other by name, they use a lucky number to recognize one another. Therefore, you will receive a linked list where each node contains a lucky number. Your task is to reverse the order of nodes in groups of \(k\) elephants. If the remaining nodes at the end are fewer than \(k\), jsut reverse the rest of them.
The I/O functions are already implemented in the given file. You only need to complete the void solve(Node* head, int l, int r)
fucntion that returns the same dummy head. Your task is to reverse the linked list in each \(k\) group.
You can ignore the warning while compiling: cast to smaller integer type 'int' from 'Node *' (aka 'struct Node *') [-Wpointer-to-int-cast]
There is a restriction: You can only modify the “next” pointer of each node to change the order. If you attempt to modify the node values or return a list containing nodes whose addresses do not belong to the original list, you may receive a wrong answer.
Images are cited from Chiikawa wiki.
The first line contains two integers \(n\) and \(k\), representing the size of the list and the group size.
The second line contains \(n\) integers \(a_1, a_2,...,a_n\), representing the lucky numbers of the elephants.
Output the modified list of lucky numbers, each followed by a space.