力扣 25. K 个一组翻转链表

45 阅读1分钟

🔗 leetcode.cn/problems/re…

题目

  • RT

思路

  • 每 k 个为一组,递归翻转

代码

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* find_head_k(ListNode* head, int k) {
        // check if len >= k
        int count = 0;
        ListNode* curr = head;
        ListNode* next_head = nullptr;
        while (curr) {
            count++;
            if (count == k) {
                next_head = curr->next;
                break;
            }
            curr = curr->next;
        }
        if (count < k) return head;

        // reverse
        curr = head->next; count = 1;
        ListNode* pre = head;
        while (curr) {
            count++;     
            ListNode* next = curr->next;
            curr->next = pre;
            pre = curr;
            if (count == k) break;
            curr = next;
        }

        head->next = find_head_k(next_head, k);

        return curr;
    }
    ListNode* reverseKGroup(ListNode* head, int k) {
        if (k == 1) return head;
        return find_head_k(head, k);
    }
};