Reverse interval list, the node K between each single linked list in reverse order, to print out a new list; finally less than the number of nodes K need not reverse; request time complexity is O (n), the additional space complexity is O (1) .

Interval list in reverse order
Description

The reverse node K between each single linked list, print a new list; finally less than the number of nodes K need not reverse; request time complexity is O (n), the additional space complexity is O (1).

Input

Input of a first embodiment with a number of behaviors, the value of each row of each test inputs separated by a space, represents a first chain length, the intermediate node values, representative of the last K.

Output

Each output behavior of the new list, the node values ​​separated by a space, not the end of the space.

Sample Input 1

2
8 1 2 3 4 5 6 7 8 3
8 a b c d e f g h 4

Sample Output 1

3 2 1 6 5 4 7 8
d c b a h g f e

import java.io.EOFException;
import java.util.*;

public class Main {
    private static int T;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        T = in.nextInt();
        in.nextLine();
        while (T-- != 0) {
            String s = in.nextLine();
            String[] dataStr = s.split(" ");

            int n = Integer.valueOf(dataStr[0]);
            if (n == 0){
                print();
                continue;
            }
            int spaceCnt = n-1;
            String[] strings = new String[n];
            for (int i = 0; i < n; i++) {
                strings[i] = dataStr[i+1];
            }
            int k = Integer.parseInt(dataStr[dataStr.length-1]);
            if (k == 0){
                for (int i = 0; i < n; i++) {
                    System.out.print(strings[i]);
                    if (spaceCnt-- != 0){
                        System.out.print(" ");
                    }
                }
                print();
                continue ;
            }
            if (k > n){
                for (int i = 0; i < n; i++) {
                    System.out.print(strings[i]);
                    if (spaceCnt-- != 0){
                        System.out.print(" ");
                    }
                }
                print();
                continue ;
            }
            int point = -1;
            while (point + k < n){
                for (int i = 0; i < k; i++) {
                    System.out.print(strings[point + k - i]);
                    if (spaceCnt-- != 0){
                        System.out.print(" ");
                    }
                }
                point += k;
            }
            if (point < n){
                for (int i = point + 1; i < n; i++) {
                    System.out.print(strings[i]);
                    if (spaceCnt-- != 0){
                        System.out.print(" ");
                    }
                }
            }
            print();
        }
    }


    private static void print(){
        if (T == 0){
            System.out.println();
        }else{
            System.out.println();
        }
    }
}

Guess you like

Origin www.cnblogs.com/billxxx/p/11768967.html