725. Split Linked List in Parts**

725. Split Linked List in Parts**

https://leetcode.com/problems/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, \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.

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].

C++ 实现 1

两年前的思路, 我觉得很赞啊.

思路: 这道题思考一下之后可以注意到使用队列保存链表的节点会是一个好想法. 比如第二个例子, 有 10 个节点, 要将其分成 3 份, 那么大小分别为 4, 3, 3. 只要确定了每个小链表的大小(代码中为 num 变量), 那么每次从队列中弹出来 num 个节点即可. 如果队列中没有元素了, 但却还要确定几个空链表(比如第一个例子), 那么只要设置空链表即可(比如下面代码中直接设置 ptr->next = nullptr.)

扫描二维码关注公众号,回复: 10027117 查看本文章
class Solution {
public:
    vector<ListNode*> splitListToParts(ListNode* root, int k) {
      	// 将链表中的节点放在队列中
        queue<ListNode*> Queue;
        auto ptr = root;
        while (ptr) {
            Queue.push(ptr);
            ptr = ptr->next;
        }

        int n = Queue.size();
        int num = 0; // num 用于记录小链表的大小
        vector<ListNode*> res;
      	// 每次产生第 i 个小链表
        for (int i = 0; i < k; ++i) {
            ListNode *dummy = new ListNode(0);
            auto ptr = dummy;
          	// j 用于统计是否到达小链表的大小
            int j = 0;
          	// 对于 num 的确定是个有意思的问题, 举个例子, 比如链表大小为 n=11, 而要
          	// 分成 k=3 份, 那么为了计算第一份的大小: 11/3 = 3, 但是由于11无法整除 3,
          	// 所以令 3+1=4 为第一份的大小; 之后剩余 11-4=7 个节点未处理, 但注意, 此时
          	// 只需要求剩下的两份, 所以 7要除以 2, 即 7/2=3, 所以第二份的大小为 3+1=4,
          	// 最后还剩下 7-4=3 个节点, 由于只需要 1 份了, 所以最后一份大小为 3/1=3.
          	// (还可以用 n=10, k=3 举例子, 发现规律)
          	// 另外注意 queue<int> 是队列(而不是优先队列), 它没有 top 方法, 它是容器配接器,
          	// 底层容器是 deque.
            n -= num;
            num = n % (k - i) ? (n / (k - i) + 1) : n / (k - i);
            while (!Queue.empty() && j < num) {
                auto node = Queue.front();
                Queue.pop();
                ptr->next = node;
                ptr = ptr->next;
                ++j;
            }
            ptr->next = nullptr;
            res.push_back(dummy->next);
        }
        return res;
    }
};
发布了455 篇原创文章 · 获赞 8 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Eric_1993/article/details/104942608