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.

题目标签:Linked List

  题目给了我们一个链表,还给了我们k,让我们把链表分成 k 个部分,使得每一个部分尽可能相等。
  首先,我们要知道链表的长度 len,利用 len 和 k 来分组。
  举例:
    1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10
 
    len = 10; k = 7 的话;
    首先计算 base =  len / k = 10 / 7 = 1;
    然后计算 leftover = len % k = 10 % 7 = 3;
    先把每一个小组的base 值填入
    [1, 1, 1, 1, 1, 1, 1]  一共有7个小组,每个小组目前有1个node
    然后把 leftover 分别加 1到每一个小组,从前到后,直到加完。
    [2, 2, 2, 1, 1, 1, 1]
    这样的话,一共有7个小组,前三个小组,每一组有2个 nodes,后面4个小组,每一组有1个node。
    这里写成 array 方便理解,答案中只需要知道 base, 然后把 leftover 加上就可以。
 
    所以根据上面的 array, 就可以把原链表分组为:
    [1 -> 2]   [3 -> 4]   [5 -> 6]   [7]   [8]   [9]   [10]
 
 
 
 

Java Solution:

Runtime beats 59.09%

完成日期:12/07/2017

关键词:singly-linked list

关键点:计算出base 和 leftover

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution
{
public ListNode[] splitListToParts(ListNode root, int k)
{
ListNode[] list = new ListNode[k];
int len = 0;
ListNode cursor = root;
int base = 0;
int leftover = 0; // count the length
while(cursor != null)
{
cursor = cursor.next;
len++;
} // calculate the base and leftover
base = len / k;
leftover = len % k;
cursor = root; // iterate each group
for(int i=0; i<k; i++)
{
list[i] = cursor; // save this group head
ListNode tail = null;
int groupSize = base; // set up correct group size
if(leftover > 0)
{
groupSize++;
leftover--;
} // iterate this group nodes
for(int j=0; j<groupSize; j++)
{
if(j == groupSize - 1) // approach to the end of this group
tail = cursor; cursor = cursor.next;
} if(groupSize > 0) // link this group tail to null
tail.next = null;
} return list;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

最新文章

  1. [css]我要用css画幅画(七) - 哆啦A梦
  2. UVA - 11584 Partitioning by Palindromes[序列DP]
  3. paramiko模块-2
  4. Ubuntu下使用Git和GitHub
  5. NOIP2012 同余方程-拓展欧几里得
  6. hdu 1496 Equations
  7. Hopfield模型
  8. HTTP服务负载均衡总结
  9. Spring源代码由浅入深系列五 GetBean
  10. asm 盘头损失,破坏
  11. Gym 101667I Slot Machines
  12. n级阶梯,每次走一步或两步,问最多有多少种走法 二叉树实现
  13. zip4j实现文件压缩与解压缩 &amp; common-compress压缩与解压缩
  14. apache-2.4.6 mod_bw-0.92 实现限速上传或下载
  15. 记录7: office 2016 Mac不能使用的解决过程
  16. 一套能体现 RBAC 的表结构设计
  17. c# 静态构造函数与构造函数的调用先后
  18. vue-cli项目开发/生产环境代理实现跨域请求+webpack配置开发/生产环境的接口地址
  19. SDIBT 2345 (3.2.1 Factorials 阶乘)
  20. 设计模式学习---UML常见关系的实现

热门文章

  1. Js上传图片并生成缩略图
  2. interface与抽象类
  3. Java中PrintStream(打印输出流)
  4. Windows Server 2008不能Ping改为允许的方法
  5. 世界上最受欢迎的10个Linux发行版
  6. JMeter怎样测试WebSocket,如何设置(一)
  7. IOS 11,UIWebView内容随状态栏高度下移,导致状态栏不透明
  8. nginx反向代理与负载均衡讲解
  9. 14Oracle Database 高级事务,游标
  10. BZOJ3124: [Sdoi2013]直径 (树形DP)