C++ Solution O(1) Space 9ms


  • 3
    X
    class Solution {
    public:
        vector<ListNode*> splitListToParts(ListNode* root, int k) {
            int len = 0;
            for (ListNode *x = root; x; ++len, x = x->next);
    
            int n = len / k, r = len % k;
            vector<ListNode*> ret(k);
            for (int i = 0; i < k && root; ++i) {
                ret[i] = root;
                for (int j = 1; j < n + (i < r); ++j)
                    root = root->next;
                ListNode *next = root->next;
                root->next = nullptr;
                root = next;
            }
            return ret;
        }
    };
    

  • 0
    S
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.