#Leetcode# 725. Split Linked List in Parts
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 
rootwill be in the range[0, 1000]. - Each value of a node in the input will be an integer in the range 
[0, 999]. kwill 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的更多相关文章
- 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 ...
 - LeetCode 725. Split Linked List in Parts(分隔链表)
		
题意:将原链表分隔成k个链表,要求所有分隔的链表长度差异至多为1,且前面的链表长度必须大于等于后面的链表长度. 分析: (1)首先计算链表总长len (2)根据len得到分隔的链表长度要么为size, ...
 - [leetcode]725. Split Linked List in Parts链表分块
		
思路很简单 按时链表的题做起来很容易犯小错误,思维要缜密 还要多练习啊 做之前最好画算法框图 public ListNode[] splitListToParts(ListNode root, in ...
 - 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 ...
 - 【LeetCode】725. Split Linked List in Parts 解题报告(Python & C++)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
 - 【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 ...
 - Python解Leetcode: 725. Split Linked List in Parts
		
题目描述:给定一个单链表,写一个函数把它分成k个单链表.分割成的k个单链表中,两两之间长度差不超过1,允许为空.分成的k个链表中,顺序要和原先的保持一致,比如说每个单链表有3个结点,则第一个单链表的结 ...
 - 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 ...
 - 725. Split Linked List in Parts
		
▶ 将一个单链表拆分为长度尽量接近的 k 段 ● 自己的代码,12 ms ■ 记链表长度为 count,目标段数为 k,quo = count / k,mod = count % k,part = m ...
 
随机推荐
- [luogu4072] 征途
			
题面 题意体面中写得很明确, 应该不用我说了, 方差的概念在初中人教版九年级数学中有所提到, 没有上过初中的同学们可以左转百度. 将序列拆为几段求最值, 我们考虑用dp来实现. 先推一下式子, 令方差 ...
 - [luogu2469] 星际竞速
			
题面  巨佬一眼就能看出这是最小路径覆盖, 我这个蒟蒻还是太弱了...  我们可以知道跳跃值为点权w[i], 两点之间距离为边权ww  对于每个点, 在最小路径覆盖问题中, 假设每个点都是一条路 ...
 - java通过反射调用有参数的方法
			
public static void eachCfg(Class Initclass,String taskType){ Field[] fields = Initclass.getDeclaredF ...
 - day2-课堂笔记
			
#面向对象 函数=方法 系统内建函数:len().id() 对象函数
 - 【转】mysql增量备份恢复实战企业案例
			
来源地址:http://seanlook.com/2014/12/05/mysql_incremental_backup_example/ 小量的数据库可以每天进行完整备份,因为这也用不了多少时间,但 ...
 - 了解linux的进程:rootfs与linuxrc
			
导读 内核启动的最后阶段启动了三个进程进程0:进程0其实就是刚才讲过的idle进程,叫空闲进程,也就是死循环.进程1:kernel_init函数就是进程1,这个进程被称为init进程.进程2:kthr ...
 - 广州商学院Python正方教务系统爬虫(获取个人信息成绩课表修改密码)
			
使用python的requests库简单爬取,使用xpath解析内容 可以获取个人信息.个人照片.成绩单和课表 github地址:https://github.com/PythonerKK/GZCC- ...
 - python+requests实现接口测试 - cookies的使用 (转载)
			
出自:https://www.cnblogs.com/nizhihong/p/6699492.html 在很多时候,发送请求后,服务端会对发送请求方进行身份识别,如果请求中缺少识别信息或存在错误的识别 ...
 - day51
			
JS基础操作 一.分支结构 1.if语句 if 基础语法 if (条件表达式) { 代码块; } // 当条件表达式结果为true,会执行代码块:反之不执行 // 条件表达式可以为普通表达式 // 0 ...
 - 文件I/O(2)
			
文件I/O(2) 文件共享 内核使用三种数据结构表示打开的文件,他们之间的关系决定了在文件共享方面一个进程对还有一个进程可能产生的影响.如图1所看到的. 1) 每一个进程在进程表中都有一个记录项.记 ...