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.
题目标签:Linked List
Java Solution:
Runtime beats 59.09%
完成日期:12/07/2017
关键词:singly-linked list
关键点:计算出base 和 leftover
/**
* 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)
{
ListNode[] list = new ListNode[k];
int len = 0;
ListNode cursor = root;
int base = 0;
int leftover = 0; // count the length
while(cursor != null)
{
cursor = cursor.next;
len++;
} // calculate the base and leftover
base = len / k;
leftover = len % k;
cursor = root; // iterate each group
for(int i=0; i<k; i++)
{
list[i] = cursor; // save this group head
ListNode tail = null;
int groupSize = base; // set up correct group size
if(leftover > 0)
{
groupSize++;
leftover--;
} // iterate this group nodes
for(int j=0; j<groupSize; j++)
{
if(j == groupSize - 1) // approach to the end of this group
tail = cursor; cursor = cursor.next;
} if(groupSize > 0) // link this group tail to null
tail.next = null;
} return list;
}
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 725. Split Linked List in Parts (分裂链表)的更多相关文章
- #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(分隔链表)
题意:将原链表分隔成k个链表,要求所有分隔的链表长度差异至多为1,且前面的链表长度必须大于等于后面的链表长度. 分析: (1)首先计算链表总长len (2)根据len得到分隔的链表长度要么为size, ...
- [leetcode]725. Split Linked List in Parts链表分块
思路很简单 按时链表的题做起来很容易犯小错误,思维要缜密 还要多练习啊 做之前最好画算法框图 public ListNode[] splitListToParts(ListNode root, in ...
- 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 ...
- 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个结点,则第一个单链表的结 ...
- [LeetCode] 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 ...
随机推荐
- swift protocol 与类继承结合时的bug
protocol CommonTrait: class { func commonBehavior() -> String } extension CommonTrait { func comm ...
- catkin_package()是什么意思?
DEPENDS 和 CATKIN_DEPENDS 用来告诉 catkin 需要将你程序包A的哪些依赖项传递给使用 find_package(...) 查找你的程序包的程序包B. 而在CMakeList ...
- 并发编程学习笔记(11)----FutureTask的使用及实现
1. Future的使用 Future模式解决的问题是.在实际的运用场景中,可能某一个任务执行起来非常耗时,如果我们线程一直等着该任务执行完成再去执行其他的代码,就会损耗很大的性能,而Future接口 ...
- 解决[disabled]="true"与formControlName冲突
import { FormBuilder } from '@angular/forms'; form; constructor(private fb: FormBuilder) { this.form ...
- jQuery元素节点的插入
jquery插入节点的的方法,总的来说有8种,但是只要学会了其中的两个就能理解全部了, 这里我们学习append()和appendTo()两个方法: append()方法是向元素的内部追加内容: &l ...
- rem2
html{font-size:50px;}body{font-size:24px;}@media screen and (min-width:320px){ html{font-size:21.333 ...
- python 使用time / datetime进行时间、时间戳、日期转换
python 使用time 进行时间.时间戳.日期格式转换 #!/usr/bin/python3 # -*- coding: utf-8 -*- # @Time : 2017/11/7 15:53 # ...
- Linux查看用户列表
cat /etc/passwd 可以查看所有用户的列表w 可以查看当前活跃的用户列表cat /etc/group 查看用户组 groups 查看当前登录用户的组内成员groups gliethttp ...
- 三 , lnmp 一键包安装使用
安装打包环境 #https://lnmp.org/----------------------------------------------------#安装wget -c http://soft ...
- Spring AOP学习(六)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...