linked list 10

1. Subject:

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.


2. Answer: The meaning of the question is to divide the linked list into "even divisions", and the length difference of each sub-linked list cannot exceed 1; similar to the idea of ​​​​balanced binary tree; 

                The simplest idea is to divide the length of the linked list by the number of shares that need to be split, and then add the remainder until the remainder is 0;

                 eg: 10 is divided into 3 parts 10/3 = 3 ,1; then 3+1,3,3; 11/3 = 3,2; then 3+1,3+1,3

3. C++ code

class Solution {
public:
    vector<ListNode*> splitListToParts(ListNode* root, int k) {
        vector<ListNode*> subListNode(k,nullptr);
        int len ​​= 0;
        ListNode *node = root;
        
        //Calculate the length of the linked list
        while(node){
            node = node->next;
            len ++;
        }
        // 10/3 = 3 r = 1;  4,3,3;  11/3 = 3,r=2; 4,4,3
        int n = len / k, r = len% k;
        node = root;
        ListNode *prev = nullptr;
        for (int i = 0; node && i < k; i++, r--) { //r-- controls how many copies need to be added by 1; i is the number of copies to be divided, and j is the length of each element .
            subListNode[i] = node;
            for (int j = 0; j < n + (r > 0); j++) { //r > 0 to control each increment by 1
                prev = node;
                node = node->next;
            }
            prev->next = nullptr; //Note
        }
        return subListNode;
    }
    
};


python code

class Solution:
    def splitListToParts(self, root, k):
        """
        :type root: ListNode
        :type k: int
        :rtype: List[ListNode]
        """
        result = []
        
        n = 0
        curr = root
        
        while curr:
            curr = curr.next
            n += 1
        
        curr = root
        
        n,r = divmod(n,k)
        
        for i in range(0,k):
            subList = []
            
            for j in range(0,n + int(i < r)): //Same as above
                subList.append(curr.val)
                curr = curr.next
            
            result.append(subList)
            
        return result


         

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325635785&siteId=291194637