【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 ...
随机推荐
- restful api编写规范
Node.js 除了用来编写 WEB 应用之外,还可以用来编写 API 服务,我们在本文中会介绍编写 Node.js Rest API 的最佳实践,包括如何命名路由.进行认证和测试等话题,内容摘要如下 ...
- Fiddler无所不能——之测试开发攻城狮必备利器
Fiddler无所不能——之测试开发攻城狮必备利器 1.模拟真实网络环境4g网.3g网络.2g网络.弱网.请求超时 开启弱网Rules——Performance——勾选Simulate Modem S ...
- go VS NET 字符串操作能力
今天拿golang 与 NET4.0 做了在字符串方面的性能比较,看看谁牛! 一.读取txt文本文件 GO的代码: readbuf, _ := ioutil.ReadFile(userFile) st ...
- PhpStorm2016.2版本 安装与破解
1.PhpStorm2016简介以及下载地址 1.1.PhpStorm介绍 PhpStorm是一个轻量级且便捷的PHP IDE,其旨在提高用户效率,可深刻理解用户的编码,提供智能代码补全 快速导 ...
- 学习笔记之windows 网络编程
WinSock2.h编程接口笔记在Qtcreater中使用系统默认的库只需要在.pro文件中添加 LIBS += -lws2_32 添加头文件#include <WinSock2.h *初始化套 ...
- Unity中几个特殊路径在各个平台的访问方式
1.文件路径Resources:Unity在发布成移动端项目后,其他文件路径都将不存在,但是如果有一些必要的资源,可以放在Resources文件夹下,因为这个文件夹下的所有资源是由Unity内部进行调 ...
- Lua学习笔记(6): 函数
Lua的函数 函数用于简化程序,当某些工作需要重复执行的时候就可以使用函数减轻工作量(虽然复制粘贴也行) 语法: function 函数名(参数列表) 函数体 return 返回值 end --结束标 ...
- 【第八章】MySQL数据库备份—逻辑备份
一.数据库备份 1.命令简介: # mysqldump -h 服务器 -u用户名 -p密码 数据库名 > 备份文件.sql1)关于数据库名: -A, --all-databases ...
- Linux内核学习笔记(6)-- 进程优先级详解(prio、static_prio、normal_prio、rt_priority)
Linux 中采用了两种不同的优先级范围,一种是 nice 值,一种是实时优先级.在上一篇粗略的说了一下 nice 值和实时优先级,仍有不少疑问,本文来详细说明一下进程优先级.linux 内核版本为 ...
- Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift
1. 摘要 训练深层的神经网络非常困难,因为在训练的过程中,随着前面层数参数的改变,每层输入的分布也会随之改变.这需要我们设置较小的学习率并且谨慎地对参数进行初始化,因此训练过程比较缓慢. 作者将这种 ...