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) {
// end 表示一个k长度后的首个节点
ListNode end = head;
for(int i =0; i<k; i++){
if( end == null){
return head;
}else{
end = end.next;
}
}
ListNode pre = null;
ListNode cur = head;
// 对当前的K长度的链表就行翻转
while( cur != end){
ListNode temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
//此时的head为当前K长度的最后一个节点,pre为第一个节点,使用递归处理最后一个节点的next
head.next = reverseKGroup(end,k);
return pre;
}
}
/*
* 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) {
// end 表示一个k长度后的首个节点
ListNode end = head;
for(int i =0; i<k; i++){
if( end == null){
return head;
}else{
end = end.next;
}
}
ListNode pre = null;
ListNode cur = head;
// 对当前的K长度的链表就行翻转
while( cur != end){
ListNode temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
//此时的head为当前K长度的最后一个节点,pre为第一个节点,使用递归处理最后一个节点的next
head.next = reverseKGroup(end,k);
return pre;
}
}