1、解题思路

  1. 分组处理:将链表分成多个长度为 k 的子链表,每组单独处理。对于每个子链表,进行翻转操作。
  2. 翻转子链表:使用头插法或迭代法翻转每个子链表。翻转后重新连接子链表与剩余链表。
  3. 处理不足 k 的组:检查剩余节点数量是否足够 k 个。如果不足,则保持原样。
  4. 连接各组:将翻转后的各组子链表正确连接起来。

2、代码实现

C++
/**
 * struct ListNode {
 *  int val;
 *  struct ListNode *next;
 *  ListNode(int x) : val(x), next(nullptr) {}
 * };
 */
class Solution {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param head ListNode类
     * @param k int整型
     * @return ListNode类
     */
    ListNode* reverseKGroup(ListNode* head, int k) {
        // write code here
        if (head == nullptr || k == 1) {
            return head;
        }

        ListNode dummy(0), *prev = &dummy, *cur = head, *next = nullptr;
        dummy.next = head;

        int length = 0;
        while (cur) {
            length++;
            cur = cur->next;
        }

        cur = head;
        for (int i = 0; i < length / k; ++i) {
            ListNode* groupPrev = prev;
            ListNode* groupStart = cur;
            for (int j = 0; j < k; ++j) {
                next = cur->next;
                cur->next = groupPrev->next;
                groupPrev->next = cur;
                cur = next;
            }
            prev = groupStart;
            prev->next = cur;
        }

        return dummy.next;
    }
};

Java
import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 *   public ListNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param head ListNode类
     * @param k int整型
     * @return ListNode类
     */
    public ListNode reverseKGroup (ListNode head, int k) {
        // write code here
        if (head == null || k == 1) {
            return head;
        }

        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode pre = dummy;
        ListNode curr = head;
        ListNode next = null;

        int length = 0;
        while (curr != null) {
            length++;
            curr = curr.next;
        }

        curr = head;
        for (int i = 0; i < length / k; i++) {
            ListNode groupPre = pre;
            ListNode groupStart = curr;
            for (int j = 0; j < k; j++) {
                next = curr.next;
                curr.next = groupPre.next;
                groupPre.next = curr;
                curr = next;
            }
            pre = groupStart;
            pre.next = curr;
        }

        return dummy.next;
    }
}

Python
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param head ListNode类 
# @param k int整型 
# @return ListNode类
#
class Solution:
    def reverseKGroup(self , head: ListNode, k: int) -> ListNode:
        # write code here
        if not head or k == 1:
            return head

        dummy = ListNode(0)
        dummy.next = head
        pre = dummy
        curr = head
        next_node = None

        length = 0
        while curr:
            length += 1
            curr = curr.next

        curr = head
        for _ in range(length // k):
            group_pre = pre
            group_start = curr
            for _ in range(k):
                next_node = curr.next
                curr.next = group_pre.next
                group_pre.next = curr
                curr = next_node
            pre = group_start
            pre.next = curr

        return dummy.next

3、复杂度分析

  • 时间复杂度:O(n),遍历链表两次(计算长度和翻转操作)。
  • 空间复杂度:O(1),仅使用固定数量的额外指针。