链表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


         

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值