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
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]
.
attention:
a = rootlen / k
b = rootlen % k
a is the basic elements that will appear in every k array.
b is the more elements that pre-b array need to append.
eg.
[1,2,3,4,5,6,7]
rootlen = 7, k = 4
a = 1
b = 2
so
loop 1, append a(1) elements from root.
[[1] [] [] []] , b > 0 => [[1,2],[],[],[]], b = b-1 (1)
loop2 append a(1) elements from root.
[[1,2],[3],[],[]], b>0 => [[1,2],[3,4],[],[]] b = b-1(0)
loop 3 ....
Runtime: 3 ms, faster than 98.84% of Java online submissions for Split Linked List in Parts.
/**
* 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) {
int rootlen = ;
ListNode[] ln = new ListNode[k];
ListNode tmp = root, tmpnext;
while(tmp != null){
tmp = tmp.next;
rootlen++; }
if(rootlen == ) return ln;
int a = rootlen % k;
int b = rootlen / k;
int cnt = ;
for(int i=; i<k; i++) {
if(root == null) continue;
tmp = root;
for(int j=; j<b-; j++){
tmp = tmp.next;
}
if(a != && b != ) {
tmp = tmp.next;
a--;
}
tmpnext = tmp.next;
tmp.next = null;
ln[cnt++] = root;
root = tmpnext;
}
return ln;
}
}
LC 725. Split Linked List in Parts的更多相关文章
- 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 ...
- #Leetcode# 725. Split Linked List in Parts
https://leetcode.com/problems/split-linked-list-in-parts/ Given a (singly) linked list with head nod ...
- 【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 (分裂链表)
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 ...
- 725. Split Linked List in Parts
▶ 将一个单链表拆分为长度尽量接近的 k 段 ● 自己的代码,12 ms ■ 记链表长度为 count,目标段数为 k,quo = count / k,mod = count % k,part = m ...
- Python解Leetcode: 725. Split Linked List in Parts
题目描述:给定一个单链表,写一个函数把它分成k个单链表.分割成的k个单链表中,两两之间长度差不超过1,允许为空.分成的k个链表中,顺序要和原先的保持一致,比如说每个单链表有3个结点,则第一个单链表的结 ...
- 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 ...
随机推荐
- IEAD工具教你创建maven项目
之前一直用的是其他的开发工具,maven到目前为止也就用了3个月,今天又时间整理一些初期的使用方法,仅供参照. 为什么要用maven 原因很简单,因为使用maven,会使得项目非常容易管理. 举个例子 ...
- 第五章、Django之多表查询进阶与事务
目录 第五章.Django之多表查询 一.聚合查询 二.分组查询 三.F与Q查询 四.查询优化 五.Django开启事务 六.自定义char字段 七.ORM常用字段 第五章.Django之多表查询 一 ...
- 1.开始认识flask
1. pip安装flask包pip install flask2.对flask最基本的使用from flask import Flask # 导入flask包 app = Flask(__name__ ...
- 关于postgres数据库部署之后,发现不能被外机连接解决办法
数据库 部署完毕之后,用其他机器的navcat连接发现不能连接,如下报错信息 于是在数据库服务器上查询是否启动正常,端口是否正常,发现都没有问题,由于之前也遇到了mysql部署之后,不能被其他机器访问 ...
- kubernetes之健康状态检测
1.说明 容器探针: kubelet 对容器执行的定期诊断 探针执行方式: LivenessProbe: 判断容器是否存活 running状态, 如果不健康kubelet就会杀掉pod,根据重启策略R ...
- YOLO---YOLOv3 with OpenCV安装与使用
Yolo v3+Opencv3.4.2安装记录 @wp20180930 目录 一.环境要求 (1)python版本的查看 (2)opencv版本的查看 二.文件下载 三.数据自测 四.问题与解决 (1 ...
- springMVC的详解
一,springmvc注解特性 1.@Controller 控制器定义在 spring 3.0 中,通过@controller 标注即可将 class 定义为一个 controller 类.为使 sp ...
- OI界的事
随时更新: 目前在中大型考试上已经因为freopen相关的锅导致此题爆零共有: 5次.并且因此参加不了提高组(菜到无人反驳) 本人:学oi半年的练习生)蒟蒻 ,擅长水红题,橙题,博客,以及电子方面. ...
- Zabbix Web 中文字体显示问题
- springbootdruidmybatismysql多数据源事务管理
springboot+druid+mybatis+mysql+多数据源事务管理 分布式事务在java中的解决方案就是JTA(即Java Transaction API):springboot官方提供了 ...