解法
package com.wangxiaohu;
public class LeetCode25 {
/**
* 每 K 个一组反转链表
* 参考:https://leetcode-cn.com/problems/reverse-nodes-in-k-group/submissions/
*
* @param head
* @param k
* @return
*/
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null) {
return head;// = return null
}
// 区间 [a, b) 包含 k 个待反转元素
ListNode a = head, b = head;
for (int i = 0; i < k; i++) {
// 不足 k 个,不需要反转,base case
if (b == null) {
return head;
}
b = b.next;
}
// 迭代反转前 k 个元素
ListNode newHead = reverse(a, b);
// 递归反转后续链表并连接起来
a.next = reverseKGroup(b, k);
return newHead;
}
/**
* 反转区间 [a, b) 的元素,注意是左闭右开
*
* @param a
* @param b
* @return
*/
private ListNode reverse(ListNode a, ListNode b) {
ListNode pre = null, cur = a, next = a;
// while 终止的条件改一下就行了
while (cur != b) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
// 返回反转后的头结点
return pre;
}
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;
}
}
}