leetcode 25 Reverse Nodes in k-Group

Posted 王坤1993

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode 25 Reverse Nodes in k-Group相关的知识,希望对你有一定的参考价值。

class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        ListNode *cur = head;
        for (int i = 0; i < k; ++i) {
            if (!cur) return head;
            cur = cur->next;
        }
        ListNode *new_head = reverse(head, cur);
        head->next = reverseKGroup(cur, k);
        return new_head;
    }
    ListNode* reverse(ListNode* head, ListNode* tail) {
        ListNode *pre = tail;
        while (head != tail) {
            ListNode *t = head->next;
            head->next = pre;
            pre = head;
            head = t;
        }
        return pre;
    }
};

以上是关于leetcode 25 Reverse Nodes in k-Group的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 25:Reverse Nodes in k-Group

LeetCode算法题python解法:25. Reverse Nodes in k-Group

LeetCode 25. Reverse Nodes in k-Group

leetcode25. Reverse Nodes in k-Group

LeetCode 25: Reverse Nodes in k-Group

leetcode 25 Reverse Nodes in k-Group