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;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章