链表10

1、题目:

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、解答: 题目的意思就是,把链表切分为"均分",每个子链表的长度差不能超过1;类似与平衡二叉树的思想; 

                最简单的思想是 链表长度除以需要切分的份数,每份再余数,直到余数为0;

                 eg:  10切分为3份    10/3 = 3 ,1;则为  3+1,3,3  ;  11/3 = 3,2;则为 3+1,3+1,3

3、C++代码

class Solution {
public:
    vector<ListNode*> splitListToParts(ListNode* root, int k) {
        vector<ListNode*> subListNode(k,nullptr);
        int len = 0;
        ListNode *node = root;
        
        //计算链表的长度
        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-- 控制由多少份需要加1;i是需要切分的份数,j为每份的元素的长度。
            subListNode[i] = node;
            for (int j = 0; j < n + (r > 0); j++) {   //r >0 来控制每份加1
                prev = node;
                node = node->next;
            }
            prev->next = nullptr;           //注意
        }
        return subListNode;
    }
    
};


python代码

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)):   //与上同理
                subList.append(curr.val)
                curr = curr.next
            
            result.append(subList)
            
        return result


         

猜你喜欢

转载自blog.csdn.net/qq_31307013/article/details/80222681