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. (转)解决office软件无法卸载也无法安装的顽固问题

    原文地址 http://jingyan.baidu.com/article/f3ad7d0fcfe32509c3345bab.html 有时会出现office下载失败,然后又无法重新安装,导致offi ...

  2. SQL关于触发器及存储过程的创建

    使用T-SQL语句来创建触发器   基本语句如下﹕ create trigger trigger_name on {table_name | view_name} {for | After | Ins ...

  3. HDU5834 Magic boy Bi Luo with his excited tree (树形DP)

    题意:一棵树有点权和边权 从每个点出发 走过一条边要花费边权同时可以获得点权 边走几次就算几次花费 点权最多算一次 问每个点能获得的最大价值 题解:好吧 这才叫树形DP入门题 dp[i][0]表示从i ...

  4. nfs服务权限配置

    nfs服务权限配置 1. 查看系统是否已经安装了服务Rpm -qa | grep nfs 2. 启动服务,并且开机自动运行Systemctl start nfsSystemctl enabled nf ...

  5. 面向对象程序设计--Java语言第二周编程题:有秒计时的数字时钟

    有秒计时的数字时钟 题目内容: 这一周的编程题是需要你在课程所给的时钟程序的基础上修改而成.但是我们并不直接给你时钟程序的代码,请根据视频自己输入时钟程序的Display和Clock类的代码,然后来做 ...

  6. java基础学习日志--Stirng内存案例

    案例一: public class test1 { public static void mb_swap(String Str1,String Str2) { String temp=Str1; St ...

  7. MarkDown语法和使用

    MarkDown语法: Markdown在线编辑器 MdEditor Markdown 语法整理大集合2017 MarkDown 数学公式 在Markdown中输入数学公式(MathJax) \(\l ...

  8. UVA - 208 Firetruck(并查集+dfs)

    题目: 给出一个结点d和一个无向图中所有的边,按字典序输出这个无向图中所有从1到d的路径. 思路: 1.看到紫书上的提示,如果不预先判断结点1是否能直接到达结点d,上来就直接dfs搜索的话会超时,于是 ...

  9. [bzoj3191][JLOI2013][卡牌游戏] (概率dp)

    Description   N个人坐成一圈玩游戏.一开始我们把所有玩家按顺时针从1到N编号.首先第一回合是玩家1作为庄家.每个回合庄家都会随机(即按相等的概率)从卡牌堆里选择一张卡片,假设卡片上的数字 ...

  10. Android BottomSheet:便捷易用的底部滑出面板(1)

    Android BottomSheet:便捷易用的底部滑出面板(1) Android BottomSheet是github上的一个第三方开源项目,其主页:https://github.com/Flip ...