import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类
* @param k int整型
* @return ListNode类
*/
public ListNode reverseKGroup (ListNode head, int k) {
// write code here
if(head == null) return null;
ListNode tail = head;
for(int i = 1;i < k && tail != null;i++){
tail = tail.next;
}
if(tail == null) return head;
ListNode next = tail.next;
reverse(head,tail);
head.next = reverseKGroup(next,k);
return tail;
}
public void reverse(ListNode head,ListNode tail){
ListNode dummy = null;
ListNode last = tail.next;
while(head != last){
ListNode next = head.next;
head.next = dummy;
dummy = head;
head = next;
}
}
}