725. Split Linked List in Parts
来源:互联网 发布:mac地址查询厂商设备 编辑:程序博客网 时间:2024/06/10 02:06
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 = 5Output: [[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 = 3Output: [[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:
root
will be in the range [0, 1000]
.[0, 999]
.k
will be an integer in the range [1, 50]
.将一个链表分成k个链表。首先求得链表的长度len,从而知道拆分后的每个链表的长度。然后按照长度拆分链表即可。注意每个链表结尾要指向null。
代码:
class Solution {public: vector<ListNode*> splitListToParts(ListNode* root, int k) { if(k == 0) return vector<ListNode*>(); vector<ListNode*> res(k, NULL); ListNode* p = root; int len = 0; while(p != NULL) { ++len; p = p->next; } p = root; int n = len % k; for(int i = 0; i < k; ++i){ res[i] = p; int m = len / k + (i < n ? 1 : 0); for(int j = 0; j < m-1 && p; ++j) { p = p->next; } if(p) { ListNode* pre = p; p = p->next; pre->next = NULL; } } return res; }};
阅读全文
0 0
- 725. Split Linked List in Parts
- 725. Split Linked List in Parts。
- 725. Split Linked List in Parts
- leetcode 725. Split Linked List in Parts
- 725. Split Linked List in Parts
- Split Linked List in Parts
- LWC 58:725. Split Linked List in Parts
- Split Linked List in Parts(LeetCode725)
- LeetCode 725 (Split Linked List in Parts)
- LeetCode-Split Linked List in Parts
- Split Linked List in Parts问题及解法
- Split Linked List in Parts:切分链表
- P16 (**)-P17 (*) Drop every Nth element from a list;Split a list into two parts.
- P17 (*) Split a list into two parts; the length of the first part is given.
- 【Linked-list专题-2】237. Delete Node in a Linked List 206. Reverse Linked List
- bubble sort in linked list....(java version)
- 【Leetcode】Reverse Linked List II in JAVA
- leetcode: Delete Node in a Linked List
- 在 Azure 中将基础结构自动化工具与虚拟机配合使用
- python中处理嵌套的字典的函数
- 安装好oracle之后在相应路径下却没有生成tnsnames.ora和listener.ora
- sql 将某个字段中的部分字符替换为特定字符
- springmvc请求参数获取的几种方法
- 725. Split Linked List in Parts
- 统计表中百分比的表示方法
- js基本知识点总结-----js数组
- 《探寻linux协议栈》之一:linux协议栈概述
- Jquery 选择器大全
- Unity3D-将美术提供的图片当做字体使用(普通图片自动生成Custom font)
- macos 重围管理员帐户
- 大话设计模式读书笔记之建造者模式
- 2.2网站的高可用架构之高可用服务