天天看點

725. Split Linked List in Parts**

725. Split Linked List in Parts**

​​https://leetcode.com/problems/split-linked-list-in-parts/​​

題目描述

Given a (singly) linked list with head node ​

​root​

​​, write a function to split the linked list into ​

​k​

​ consecutive linked list “parts”.

The length of each part should be as equal as possible: no two parts should have a size differing by more than 1. This may lead to some parts being null.

The parts should be in order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal parts occurring later.

Return a List of ListNode’s representing the linked list parts that are formed.

Examples ​

​1->2->3->4, k = 5 // 5 equal parts [ [1], [2], [3], [4], null ]​

Example 1:

Input:
root = [1, 2, 3], k = 5
Output: [[1],[2],[3],[],[]]
Explanation:
The input and each element of the output are ListNodes, not arrays.
For example, the input root has root.val = 1, root.next.val = 2, \root.next.next.val = 3, and root.next.next.next = null.
The first element output[0] has output[0].val = 1, output[0].next = null.
The last element output[4] is null, but it's string representation as a ListNode is [].      

Example 2:

Input: 
root = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3
Output: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
Explanation:
The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts.      

Note:

  • The length of root will be in the range​

    ​[0, 1000]​

    ​.
  • Each value of a node in the input will be an integer in the range​

    ​[0, 999]​

    ​.
  • ​k​

    ​​ will be an integer in the range​

    ​[1, 50]​

    ​.

C++ 實作 1

class Solution {
public:
    vector<ListNode*> splitListToParts(ListNode* root, int k) {
        // 将連結清單中的節點放在隊列中
        queue<ListNode*> Queue;
        auto ptr = root;
        while (ptr) {
            Queue.push(ptr);
            ptr = ptr->next;
        }

        int n = Queue.size();
        int num = 0; // num 用于記錄小連結清單的大小
        vector<ListNode*> res;
        // 每次産生第 i 個小連結清單
        for (int i = 0; i < k; ++i) {
            ListNode *dummy = new ListNode(0);
            auto ptr = dummy;
            // j 用于統計是否到達小連結清單的大小
            int j = 0;
            // 對于 num 的确定是個有意思的問題, 舉個例子, 比如連結清單大小為 n=11, 而要
            // 分成 k=3 份, 那麼為了計算第一份的大小: 11/3 = 3, 但是由于11無法整除 3,
            // 是以令 3+1=4 為第一份的大小; 之後剩餘 11-4=7 個節點未處理, 但注意, 此時
            // 隻需要求剩下的兩份, 是以 7要除以 2, 即 7/2=3, 是以第二份的大小為 3+1=4,
            // 最後還剩下 7-4=3 個節點, 由于隻需要 1 份了, 是以最後一份大小為 3/1=3.
            // (還可以用 n=10, k=3 舉例子, 發現規律)
            // 另外注意 queue 是隊列(而不是優先隊列), 它沒有 top 方法, 它是容器配接器,
            // 底層容器是 deque.
            n -= num;
            num = n % (k - i) ? (n / (k - i) + 1) : n / (k - i);
            while (!Queue.empty() && j < num) {
                auto node = Queue.front();
                Queue.pop();
                ptr->next = node;
                ptr = ptr->next;
                ++j;
            }
            ptr->next = nullptr;
            res.push_back(dummy->next);
        }
        return res;
    }
};