/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if(k <= 1) return head;
ListNode dummy = new ListNode(0, head);
ListNode tail = dummy;
while(tail.next != null) {
int count = k;
ListNode temp = tail;
tail = tail.next;
ListNode temp2 = tail;
while(temp2 != null && count > 0) {
temp2 = temp2.next;
count--;
}
if(count > 0) return dummy.next;
count = k-1;
while(count > 0) {
ListNode cur = tail.next;
tail.next = cur.next;
cur.next = temp.next;
temp.next = cur;
count--;
}
}
return dummy.next;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。