25. Reverse Nodes in k-Group***

25. Reverse Nodes in k-Group***

https://leetcode.com/problems/reverse-nodes-in-k-group/

题目描述

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

Example:

Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5

Note:

  • Only constant extra memory is allowed.
  • You may not alter the values in the list’s nodes, only nodes itself may be changed.

C++ 实现 1

迭代的方法. reverse 的方法不多说, 将 [head, end) 范围内的节点进行翻转. 再来看迭代的过程, 当写好了 reverse 函数, 之后就是确认各个大小为 k 的 Group, 用 n 来计数在 [head, tail] 范围内的节点的个数. (这段代码中 if (n == k) break; 放在哪里需要斟酌, 要想清楚). 当 while 结束时, 需要判断 n 是否等于 k, 以便确认 [head, tail] 这个 Group 的节点个数是否达到 k. 如果是的话, 那么就需要翻转 [head, tail->next) 范围内的节点. 否则就不用翻转. 之后更新 p 以及 head 也非常关键; 如果这个 Group 翻转了, 此时 head 将指向这个 Group 的最后一个节点, 因此, 此时 phead 应该分别指向 head 以及 head->next.

class Solution {
private:
    ListNode* reverse(ListNode *head, ListNode *end) {
        auto prev = end;
        while (head != end) {
            auto tmp = head->next;
            head->next = prev;
            prev = head;
            head = tmp;
        }
        return prev;
    }
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        ListNode *dummy = new ListNode(0);
        auto p = dummy;
        while (head) {
            auto tail = head;
            int n = 1;
            // 统计在 [head, tail] 范围内节点的个数
            while (tail->next) {
                if (n == k) break;
                ++ n;
                tail = tail->next;
            }
            if (n == k) p->next = reverse(head, tail->next);
            else p->next = head;
            p = head;
            head = head->next;
        }
        return dummy->next;
    }
};

C++ 实现 2

使用递归的方法. reverse 方法和 C++ 实现 1 相同. 下面代码中, 翻转 [head, tail) 内的节点.

class Solution {
private:
    ListNode* reverse(ListNode *head, ListNode *end) {
        auto prev = end;
        while (head != end) {
            auto tmp = head->next;
            head->next = prev;
            prev = head;
            head = tmp;
        }
        return prev;
    }
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        if (!head || !head->next) return head;
        auto tail = head;
        for (int i = 0; i < k; ++ i) {
            if (!tail) return head; // 如果 Group 大小不够 k, 那么直接退出.
            tail = tail->next;
        }
        auto newhead = reverse(head, tail); // 翻转后, head 为当前 Group 的最后一个节点
        head->next = reverseKGroup(tail, k);
        return newhead;
    }
};

C++ 实现 3

如果忽视题目中关于额外空间的限制, 直接用 Stack …

class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        if (!head)
            return nullptr;

        stack<ListNode*> Stack;
        auto end = head;
        for (int i = 0; i < k; ++i) {
            if (!end)
                return head;
            Stack.push(end);
            end = end->next;
        }

        auto post = end;
        ListNode *dummy = new ListNode(0);
        auto path = dummy;
        while (!Stack.empty()) {
            path->next = Stack.top();
            Stack.pop();
            path = path->next;
        }
        path->next = reverseKGroup(post, k);
        ListNode *res = dummy->next;
        delete dummy;
        return res;
    }
};
发布了455 篇原创文章 · 获赞 8 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Eric_1993/article/details/105009843
今日推荐