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;
}
}
}

最新文章

  1. Ubuntu Filezilla FTP Client 安装
  2. 在CentOS上搭建apache和PHP服务器环境(转)
  3. zstu-3769 数回文子串
  4. AutoMapper使用笔记
  5. CENTOS LINUX查询内存大小、频率
  6. leetcode Triangle及其思考
  7. 是时候学习Android分屏开发了
  8. android 获取手机号
  9. node.js模块之http模块
  10. 将表单数据转化为json数据
  11. OSI 七层模型和 TCP/IP 协议比较
  12. Spring的文件上传
  13. 【死磕Java并发】-----Java内存模型之happens-before
  14. 剑指C++面试
  15. 【转载】Sqlserver的SQL语句实现分页查询
  16. Sorl搜索技术
  17. spring boot IDEA 开发微服务(二)
  18. 【Ray Tracing The Next Week 超详解】 光线追踪2-3
  19. 微信小程序之顶部固定和底部固定
  20. 卸载 PrestaShop 1.7

热门文章

  1. Linux IO多路复用 select
  2. android studio 调试技巧(简直太好用)
  3. NoSQL入门第一天——NoSQL入门与基本概述
  4. 20155207王雪纯 《Java程序设计》实验三 敏捷开发与XP实践 实验报告
  5. 20155325 2016-2017-2 《Java程序设计》第3周学习总结
  6. 20155334 实验四:Android程序设计
  7. 【转载】ID3DXSPRITE接口简单使用
  8. Elasticsearch6.2集群搭建
  9. shell 参数
  10. sublime3配置java开发环境