/**
* 简单繁琐暴力解法
*
* @param head
* @param k
* @return
*/
public static ListNode reverseKGroup(ListNode head, int k) {
if (head == null) {
return null;
}
int len = 0;
ListNode tail = head;
while (tail != null) {
len++;
tail = tail.next;
}
if (len < k) {
return head;
}
ListNode next;
ListNode pre = null;
int i = 0;
while (head != null) {
next = head.next;
head.next = pre;
pre = head;
head = next;
i++;
if (i % k == 0) {
break;
}
}
tail = pre;
while (tail.next != null) {
tail = tail.next;
}
len = len - i;
while (true) {
i = 0;
if (len < k) {
tail.next = head;
break;
}
ListNode pre1 = null;
while (head != null) {
next = head.next;
head.next = pre1;
pre1 = head;
head = next;
i++;
len--;
if (i % k == 0) {
break;
}
}
tail.next = pre1;
tail = pre1;
while (tail.next != null) {
tail = tail.next;
}
}
return pre;
}