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)
{
//1.先找到结尾的结点
ListNode tail = head;
for(int i = 0;i < k;i++)
{
if(tail == null)
{
return head;
}
tail = tail.next;
}
//2.然后找到起始的点
ListNode pre = null;
ListNode cur = head;
ListNode next = null;
//3.开始翻转这一组
while(cur != tail)
{
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
//4.再处理翻转后的结尾
head.next = reverseKGroup (tail,k);
//5.返回开头的结点
return pre;
}
}