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) {
if(head == null){
return null;
}
ListNode temp = head;
return dfs(temp,k);
}
ListNode dfs(ListNode root,int k){
if(root == null){
return null;
}
ListNode prev = null;
ListNode cur = root;
ListNode next = root;
ListNode temp = root;
int count = k;
while(temp != null && count > 0){
temp = temp.next;
count--;
}
if(count > 0){
return root;
}
ListNode h = temp;
int a = k;
while(next != null && a > 0){
next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
a--;
}
root.next = dfs(h,k);
return prev;
}
}
/*
* 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) {
if(head == null){
return null;
}
ListNode temp = head;
return dfs(temp,k);
}
ListNode dfs(ListNode root,int k){
if(root == null){
return null;
}
ListNode prev = null;
ListNode cur = root;
ListNode next = root;
ListNode temp = root;
int count = k;
while(temp != null && count > 0){
temp = temp.next;
count--;
}
if(count > 0){
return root;
}
ListNode h = temp;
int a = k;
while(next != null && a > 0){
next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
a--;
}
root.next = dfs(h,k);
return prev;
}
}