import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @param k int整型 
     * @return ListNode类
     */
    public static ListNode reverseKGroup(ListNode head, int k) {
        ListNode virtualRoot = new ListNode(0);
        virtualRoot.next = head;
        ListNode pre = virtualRoot;
        ListNode temp, start = pre.next;

        int count = 0;
        while (head != null) {
            count++;
            head = head.next;
        }
        for (int i = 0; i < count / k; i++) {
            for (int j = 1; j < k; j++) {
                temp = start.next;
                start.next = temp.next;
                temp.next = pre.next;
                pre.next = temp;
            }
            pre = start;
            start = start.next;
        }
        return virtualRoot.next;
    }
}