https://leetcode.com/problems/split-linked-list-in-parts/

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.

Note:

  • The length of root will be in the range [0, 1000].
  • Each value of a node in the input will be an integer in the range [0, 999].
  • k will be an integer in the range [1, 50].

代码:

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<ListNode*> splitListToParts(ListNode* root, int k) {
vector<ListNode*> ans(k);
ListNode* head = root;
int len = 1;
while(head && head -> next) {
head = head -> next;
len ++;
}
int ave = len / k;
int yu = len % k;
for(int i = 0; i < k && root; i ++) {
ans[i] = root;
for(int j = 1; j < ave + (i < yu); j ++)
root = root -> next; ListNode* t = root -> next;
root -> next = NULL;
root = t;
} return ans;
}
};

  睡到自然醒 再赖一会床简直不能再美好了 很惋惜这个假期真的真的浪费了很多时间和精神 希望一切都好吧 

#Leetcode# 725. Split Linked List in Parts的更多相关文章

  1. 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 ...

  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. 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 ...

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

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

  6. 【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 ...

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

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

  8. 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 ...

  9. 725. Split Linked List in Parts

    ▶ 将一个单链表拆分为长度尽量接近的 k 段 ● 自己的代码,12 ms ■ 记链表长度为 count,目标段数为 k,quo = count / k,mod = count % k,part = m ...

随机推荐

  1. Apache服务器的安装与配置

    文档:http://httpd.apache.org/docs/2.4/ 指令:http://httpd.apache.org/docs/2.4/mod/core.html 一.配置文件 语法 * 主 ...

  2. C++使用默认参数的构造函数

    我们可以想象一个这样的场景:某一天书店整理库存,发现了一些非常老的书,为了尽快清空库存,店主想了一下,决定开展一个大甩卖活动,所有的这些书全部以五美元的价格出售.此时如果需要尽快将这些书的信息录入到书 ...

  3. Android源代码下载以及异常查找网站推荐

    源代码下载:https://github.com/ 异常查找:http://stackoverflow.com/

  4. 第三次作业:结对编程--实现表格在APP的导入和显示

    031302517 031302319 ps:共同完成一篇随笔,文章中的第一人称我(517),队友(319) 一.功能分析+实现思路+结队讨论 这里我将功能分析和实现思路还有结对过程中的一些讨论结合在 ...

  5. VBA读取、增加自定义和修改文档属性

    读取系统文档属性 Sub read()On Error Resume Nextrw = 1Worksheets(1).ActivateFor Each p In ActiveWorkbook.Buil ...

  6. -bash: start-all.sh: 未找到命令

    解决方案:以root权限进入,找到hadoop安装的目录,进入sbin目录下 输入命令#start-all.sh 出现错误:-bash: start-all.sh: 未找到命令 百度了一下:原来需要输 ...

  7. 【小程序】text-indent设置

    要想文本首行缩进,则需要设置以下 1. 文本以<view>标签包裹 (可选) 2. 该标签设置margin值 3.以上设置text-indent才会成功

  8. mysql图形化界面MySQL_Workbench

    1,下载最新版本的MySQL Workbench,下载地址: http://www.mysql.com/downloads/workbench/   2,安装Workbench的依赖组件两个 http ...

  9. 一个IOS自动化打包的脚本

    网上找了一个脚本,在其中进行了修改,只需要一条命令就可以了 支持自动导入配置文件 支持自动安装p12证书 支持自动修改版本号和build版本号 支持自动修改app显示名称 支持自动修改bundle i ...

  10. 20155237 《JAVA程序设计》实验三(敏捷开发与XP实践)实验报告

    20155237 <JAVA程序设计>实验三(敏捷开发与XP实践)实验报告 实验内容 敏捷开发与XP实践 XP基础 XP核心实践 相关工具 实验要求 1.没有Linux基础的同学建议先学习 ...