ListNodereverseKGroup(ListNode head,int k){if(head ==null)returnnull;// 区间 [a, b) 包含 k 个待反转元素ListNode a, b;
a = b = head;for(int i =0; i < k; i++){// 不足 k 个,不需要反转,base caseif(b ==null)return head;
b = b.next;}// 反转前 k 个元素ListNode newHead =reverse(a, b);// 递归反转后续链表并连接起来
a.next =reverseKGroup(b, k);return newHead;}// 反转区间 [a, b) 的元素,注意是左闭右开 ListNodereverse(ListNode a,ListNode b){ListNode pre, cur, nxt;
pre =null; cur = a; nxt = a;// while 终止的条件改一下就行了while(cur != b){
nxt = cur.next;
cur.next = pre;
pre = cur;
cur = nxt;}// 返回反转后的头结点return pre;}