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. Hibernate框架之Criteria 详解

    自从学数据库以来,从SQL查询-HQL查询-到Criteria 查询.有人问我:掌握一种查询语句不就可以吗,为什么还要学Criteria 查询?我回答道:用到特定于数据库的SQL 语句,程序本身会依赖 ...

  2. Erwin 带注释(comment )

    1. Database>Pre & Post Script > Model-level %ForEachTable() { alter TABLE %TableName COMME ...

  3. Day 14B 网络应用开发

    网络应用开发 发送电子邮件 在即时通信软件如此发达的今天,电子邮件仍然是互联网上使用最为广泛的应用之一,公司向应聘者发出录用通知.网站向用户发送一个激活账号的链接.银行向客户推广它们的理财产品等几乎都 ...

  4. lombok无法解析log

    首先确认开发工具是否安装lombok,已安装的话打开lombok插件页,选择update, 然后重启idea.

  5. mysql中InnoDB与MyISAM的区别

    两者的区别: 1. InnoDB支持事务,MyISAM不支持,对于InnoDB每一条SQL语言都默认封装成事务,自动提交,这样会影响速度,所以最好把多条SQL语言放在begin和commit之间,组成 ...

  6. nginx代理标准配置

    #nginx开启的进程数worker_processes   4;     #4核CPU   #定义全局错误日志定义类型,[debug|info|notice|warn|crit]error_log  ...

  7. oracle 清理跟踪文件.trc .trm

    trc,trm文件介绍:trc:系统的跟踪文件(trace),当系统启动时或运行过程中出现错误时,系统会自动记录跟踪文件到指定的目录,以便于检查,这些文件需定期维护删除.trm:伴随着.trc文件产生 ...

  8. Liunx学习笔记(三) 文件权限

    一.文件权限 1.查看文件权限 (1)文件权限 在 Linux 中对于文件有四种访问权限,列举如下: 可读取:r,Readable 可写入:w,Writable 可执行:x,Execute 无权限:- ...

  9. BC in fluent

    Boundary conditions in Fluent Table of Contents 1. Boundary Conditions (BC) 1.1. Turbulence Paramete ...

  10. 恶补数论(二) Baby-Step-Giant-Step 大步小步求离散模对数

    知识概述 好吧,我承认这是我初三寒假就听过的知识,然而我现在早就高一了(又是寒假,只不过我已经在省选了...) 额,这是求离散模对数的一种算法 也就是求满足方程a^x≡b(mod p)的最小的x(其中 ...