zoukankan      html  css  js  c++  java
  • [LeetCode] 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, 
    oot.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].

    要求将链表进行分割,使分割的部分尽可能一样,如果不是平均分,应使每部分大小不相差1,如果要求的分割段数小于链表的元素数,则末尾用nullptr填充。
    思路:
    要求对链表进行分割,所以需要直到链表的总元素数len。这就要遍历链表求出。
    然后根据len和要求分割的段数k,求出平均分的情况下每段的元素数len / k,以及第k部分有多少个元素。将这几个元素分配到平均分配到最前面的几个分段中即可。
    因为题目要求结果数组中每个部分要用链表表示,所以需要一个哨兵prev来指定每个部分的结尾部分。在第一个for循环中,确定分段数以及前r个分段中元素比平均值多1。
    在第二个for循环中确定每个分段中的元素。
    if来判断分段结尾。

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<ListNode*> splitListToParts(ListNode* root, int k) {
            vector<ListNode*> res(k, nullptr);
            int len = 0;
            for (auto curr = root; curr != nullptr; curr = curr->next)
                len++;
            int n = len / k, r = len % k;
            ListNode* node = root;
            ListNode* prev = nullptr;
            for (int i = 0; i < k; i++, r--) {
                res[i] = node;
                for (int j = 0; j < n + (r > 0); j++) {
                    prev = node;
                    node = node->next;
                }
                if (prev != nullptr)
                    prev->next = nullptr;
            }
            return res;
        }
    };
    // 9 ms
  • 相关阅读:
    Mysql添加用户与授权
    php导出数据到csv
    mysql导入数据和导出数据
    null在mysql中的不同表现
    乌班图安装Lnmp环境
    php框架路由美化后提示No input file specified
    cookie删除失效问题
    MongoDb安装
    MongoDb简介
    位置索引介绍
  • 原文地址:https://www.cnblogs.com/immjc/p/7828507.html
Copyright © 2011-2022 走看看