【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
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]
.
Tips:给定一个单链表,以及一个整数k。将链表平均分成k份,要求每份之间的结点数只差不能大于1(靠前的几份结点数目大于或等于靠后的结点数)。举例如下:
root = [1, 2, 3, 4, 5].k=3;
Output: [[1, 2],[3, 4], [5].
思路:先求出单链表的长度len。small=len/k可以表示每份中包含的结点最小值(如上例,len=5,k=3,5/3=1 则每份中至少包含一个结点)。
len%k可以表示前len%k份中包含的结点数组要比small大1.可以理解为,每一份分得一个结点之后,剩余的结点,分到从前向后的每一份中。(如上例,len%k=5%3=2,则前两份结点数为small+1=2.)
public ListNode[] splitListToParts(ListNode root, int k) {
ListNode[] arr = new ListNode[k];
ListNode newHead = root;
int len = 0;
while (newHead != null) {
len++;
newHead = newHead.next;
}
int small = len / k;// 每组中结点数至少为 small
int num = len % k;// 前num组中结点数多一个 small+1
ListNode pre = new ListNode(-1);
pre.next=root;
ListNode cur=root;
int i=0;
for(;i<num && cur!=null;i++){
arr[i]=cur;
for(int j=0;j<=small;j++){
pre=cur;
cur=cur.next;
}
pre.next=null;
} for( i=num;i<k&& cur!=null;i++){
arr[i]=cur;
for(int j=0;j<small;j++){
pre=cur;
cur=cur.next;
}
pre.next=null;
}
return arr;
}
测试代码:
public static void main(String[] args) {
ListNode root = new ListNode(1);
ListNode node1 = new ListNode(1);
ListNode node2 = new ListNode(2);
ListNode node3 = new ListNode(3);
ListNode node4 = new ListNode(4);
root.next=node1;
node1.next=node2;
node2.next=node3;
node3.next=node4;
ListNode nu=null;
node4.next=nu;
int k = 3;
L725SplitLinkedListInParts l725 = new L725SplitLinkedListInParts();
ListNode[] ans = l725.splitListToParts(root, k);
for (int i = 0; i < ans.length; i++) {
System.out.println("~~~~~~" + i + "~~~~~~~~");
while (ans[i] != null) {
System.out.println(ans[i].val);
ans[i] = ans[i].next;
}
}
}
【Leetcode】725. Split Linked List in Parts的更多相关文章
- 【LeetCode】725. Split Linked List in Parts 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Python解Leetcode: 725. Split Linked List in Parts
题目描述:给定一个单链表,写一个函数把它分成k个单链表.分割成的k个单链表中,两两之间长度差不超过1,允许为空.分成的k个链表中,顺序要和原先的保持一致,比如说每个单链表有3个结点,则第一个单链表的结 ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 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
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】1221. Split a String in Balanced Strings 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 日期 题目地址:https://leetcode ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】203. Remove Linked List Elements 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 递归 日期 题目地址:https://lee ...
随机推荐
- Python面向对象总结及类与正则表达式
Python3 面向对象 一丶面向对象技术简介 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 方法:类中定义的函数. 类变 ...
- hadoop运维笔记
一. 故障处理部分 1.1. spark提交任务报错java.lang.NoClassDefFoundError: com/alibaba/fastjson/JSON 报错原因: 在运行环境没有找 ...
- HIVE-分桶表的详解和创建实例
我们学习一下分桶表,其实分区和分桶这两个概念对于初学者来说是比较难理解的.但对于理解了的人来说,发现又是如此简单. 我们先建立一个分桶表,并尝试直接上传一个数据 buckets row format ...
- 与Linux的第一次遭遇
我的与linux首次遭遇战 虚拟机安装 安装虚拟机我遇到的问题个数可以缩减到1--我几乎没遇到安装虚拟机的问题!我严格按照老师给的链接去下载那个VirtualBox,尽管那个网页是全英文的,但是我像看 ...
- XMAPP 的安装与配置
1.XMAPP简介 1.1.XAMPP(Apache+MySQL/MariaDB+PHP+Perl) 开头的X代表X-OS,代表可以在任何常见操作系统下使用,包括Windows.Mac.Linux ...
- 20155310 《Java程序设计》实验三(敏捷开发与XP实践)实验报告
20155310 <Java程序设计>实验三(敏捷开发与XP实践)实验报告 实验内容 1.XP基础 2.XP核心实践 3.相关工具 实验步骤 (一)敏捷开发与XP 1.敏捷开发 敏捷开发( ...
- 20155327 2016-2017-2 《Java程序设计》第一周学习总结
20155327 2016-2017-2 <Java程序设计>第一周学习总结 教材学习内容总结 浏览教材,根据自己的理解每章提出一个问题 1.JAVA SE中JVM,JRE与JDK分别是什 ...
- 20155328 2016-2017-2 《Java程序设计》 第一周学习总结
20155328 2016-2017-2 <Java程序设计> 第一周学习总结 教材学习内容总结 本周学习目标是浏览<Java学习笔记>中的十八章,其中第一章和第二章认真学习, ...
- 多级反馈序列c语言模拟实现
多级反馈队列调度算法: 1.设置多个就绪队列,并给队列赋予不同的优先级数,第一个最高,依次递减. 2.赋予各个队列中进程执行时间片的大小,优先级越高的队列,时间片越小. 3.当一个新进程进入内存后,首 ...
- 打豪车应用:uber详细攻略(附100元优步uber优惠码、uber优惠券、优步优惠码、优步优惠券)
在嘀嘀打车和快的打车交战热闹的时候,美国的打车应用uber进入中国.与在美国以个人司机注册做 Uber 司机为主的模式不同,Uber 在中国采用与租车公司合作.由租车公司提供车辆和司机的模式,同时中文 ...