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/

LeetCode 725. Split Linked List in Parts (分裂链表)的更多相关文章

  1. #Leetcode# 725. Split Linked List in Parts

    https://leetcode.com/problems/split-linked-list-in-parts/ Given a (singly) linked list with head nod ...

  2. LeetCode 725. Split Linked List in Parts(分隔链表)

    题意:将原链表分隔成k个链表,要求所有分隔的链表长度差异至多为1,且前面的链表长度必须大于等于后面的链表长度. 分析: (1)首先计算链表总长len (2)根据len得到分隔的链表长度要么为size, ...

  3. [leetcode]725. Split Linked List in Parts链表分块

    思路很简单  按时链表的题做起来很容易犯小错误,思维要缜密 还要多练习啊 做之前最好画算法框图 public ListNode[] splitListToParts(ListNode root, in ...

  4. 725. Split Linked List in Parts把链表分成长度不超过1的若干部分

    [抄题]: Given a (singly) linked list with head node root, write a function to split the linked list in ...

  5. LC 725. Split Linked List in Parts

    Given a (singly) linked list with head node root, write a function to split the linked list into k c ...

  6. 【LeetCode】725. Split Linked List in Parts 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【Leetcode】725. Split Linked List in Parts

    Given a (singly) linked list with head node root, write a function to split the linked list into k c ...

  8. Python解Leetcode: 725. Split Linked List in Parts

    题目描述:给定一个单链表,写一个函数把它分成k个单链表.分割成的k个单链表中,两两之间长度差不超过1,允许为空.分成的k个链表中,顺序要和原先的保持一致,比如说每个单链表有3个结点,则第一个单链表的结 ...

  9. [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 c ...

随机推荐

  1. RabbitMQ 为什么需要信道?为什么不是TCP直接通信?

    交换器和队列的关系 1. 交换器是通过路由键和队列绑定在一起的,如果消息拥有的“路由键”跟队列和交换器的“路由键”匹配,那么消息就会被路由到该绑定的队列当中去 2. 也就是说,消息到队列的过程中,消息 ...

  2. Vue.js——router-link阻止click事件

    router-link 只能单纯做路由跳转 https://segmentfault.com/q/1010000007896386

  3. Farseer.net轻量级开源框架 中级篇:事务的使用

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: Where条件的终极使用 下一篇:Farseer.net轻量级开源框架 中级篇: ...

  4. Spring注解的步骤

    Spring框架提供DI(属性注解)和IOC(类/Bean的注解)注解. 注解:标注.注入和解析.解释;标注和解释一部分代码的作用在框架中:就是配置文件的另外一种实现方式@Type.@Taget;减少 ...

  5. POJ_2255_Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12342   Accepted: 7712 De ...

  6. Java 8 和 Java 9部分区别

    Java 8 和 Java 9中 concurrent 包有了一些改变, 本文对这些改变做了汇总.Java 8 中 Concurrent package的改变java.util.concurrent中 ...

  7. IOS 11,UIWebView内容随状态栏高度下移,导致状态栏不透明

    解决方案: 方法1:在html中设置 <meta name="viewport" content="viewport-fit=cover,maximum-scale ...

  8. vue之基础---组件基础

    (1)基本示例 Vue组件示例 /* 先注册组件,定义一个名为button-component的新组件 */ Vue.component('button-component',{ data:funct ...

  9. 09Oracle Database 数据表数据插入,更新,删除

    Oracle Database 数据表数据插入,更新,删除 插入数据 Insert into table_name(column) values(values); insert into studen ...

  10. Monkey进行测试时如何屏蔽掉状态栏和音量键

    我在学习的过程中使用简单的点击命令总是会触发到音量键和状态栏,由于我的测试机是虚拟按键所以也会触碰到 接下来说一下解决办法 全屏状态  adb shell settings put global po ...