```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
ListNode endNode=head;
ListNode tempNode=head;
ListNode newHead=null;
ListNode endNodeNext=head;
//获得翻转后的尾节点所指的下一个节点
for(int i=0;i<k;i++){
if(tempNode==null){
return head;
}
tempNode=tempNode.next;
}
endNodeNext=tempNode;
//翻转
for(int i=0;i<k;i++){
tempNode=head;
head=head.next;
tempNode.next=newHead;
newHead=tempNode;
}
//递归
endNode.next=reverseKGroup(endNodeNext,k);
return newHead;
}
}