13419 - I hate New Year   

Description

Although you think that Christmas is the last torture this year, you found that you have to send red envelope to all of your cousins because your mom "suggest" you.

Since the number of money is also a non-unique integer,you want to follow the rule in 13412 - I hate Christmas but with some modification.

First, you rearrange the order with the following rule:

Given the head of the linked list containing multiple groups (the size of each group is k ), reverse the nodes inside each group at a time.

k is a positive integer and is less than or equal to the length of the linked list.

If the number of nodes is not a multiple of k, then the left-out nodes in the end should remain the same order.

You are not allowed to alter the values in each node.

 

For example, when k = 2:

 

Next, different from the 13412 - I hate Christmas, you have to reverse the whole list so the kids who want to get the red envelope first will become the last :) .

For example,

Note that it is a partial-judge problem that you need to implement the function:

Node* Solver(Node* head, int k)

Input

The first line is composed of a sequence of integer(1<= val <=100000000), which represents the values of the linked list.

The input stops when read in '-1' ('-1' is not a part of the 2 linked-lists).

The second line is k,  the size of the group that will be reversed.(1 <= k <= length of the linked list)

Output

The values of the final reversed linked-list.

The values are separated by a whitespace and the output is also ended with a whitespace.

Sample Input  Download

Sample Output  Download

Partial Judge Code

13419.c

Partial Judge Header

13419.h

Tags




Discuss