#Leetcode# 725. Split Linked List in Parts
https://leetcode.com/problems/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
rootwill be in the range[0, 1000]. - Each value of a node in the input will be an integer in the range
[0, 999]. kwill be an integer in the range[1, 50].
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<ListNode*> splitListToParts(ListNode* root, int k) {
vector<ListNode*> ans(k);
ListNode* head = root;
int len = 1;
while(head && head -> next) {
head = head -> next;
len ++;
}
int ave = len / k;
int yu = len % k;
for(int i = 0; i < k && root; i ++) {
ans[i] = root;
for(int j = 1; j < ave + (i < yu); j ++)
root = root -> next; ListNode* t = root -> next;
root -> next = NULL;
root = t;
} return ans;
}
};
睡到自然醒 再赖一会床简直不能再美好了 很惋惜这个假期真的真的浪费了很多时间和精神 希望一切都好吧
#Leetcode# 725. Split Linked List in Parts的更多相关文章
- 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(分隔链表)
题意:将原链表分隔成k个链表,要求所有分隔的链表长度差异至多为1,且前面的链表长度必须大于等于后面的链表长度. 分析: (1)首先计算链表总长len (2)根据len得到分隔的链表长度要么为size, ...
- [leetcode]725. Split Linked List in Parts链表分块
思路很简单 按时链表的题做起来很容易犯小错误,思维要缜密 还要多练习啊 做之前最好画算法框图 public ListNode[] splitListToParts(ListNode root, 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个结点,则第一个单链表的结 ...
- 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 ...
- 725. Split Linked List in Parts
▶ 将一个单链表拆分为长度尽量接近的 k 段 ● 自己的代码,12 ms ■ 记链表长度为 count,目标段数为 k,quo = count / k,mod = count % k,part = m ...
随机推荐
- P4279 [SHOI2008]小约翰的游戏
嘟嘟嘟 一道博弈论经典题,nim游戏. 只不过要考虑有奇数个石子为1的堆的时候,为Brother赢.剩下就是nim游戏了. 极简代码 #include<cstdio> using name ...
- gatewayworker中使用tcp协议连接硬件设备获取数据报错解决办法!
运行后过段时间报错, Warning: Error while sending STMT_PREPARE packet. PID=1776 in D:\phpStudy\WWW\api\mysql-m ...
- Python2.7-copy_reg
copy_reg 模块,提供了在 pickle 或是 copy 特定对象时,可以运行一个指定的函数,作为对象的构造器 模块方法: copy_reg.constructor(object):声明一个可调 ...
- Windows/Linux获取当前运行程序的绝对路径
windows 获取当前运行程序的绝对路径(.exe) GetModuleFileNameA()函数获取绝对路径,不过文件路径中的反斜杠需要进行替换. ]; GetModuleFileNameA(NU ...
- 输入5个学生的信息(包括学号,姓名,英语成绩,计算机语言成绩和数据库成绩), 统计各学生的总分,然后将学生信息和统计结果存入test.txt文件中
题目分析: 1.首先想到的是数组存放数据,数组肯定是String类型. 2.String类型的数组,5行6列.要把从第0行第2列到第4行第4列的数据取出转换成数值型,再统计三科总分.最后把计算出的总分 ...
- GrowingIO接入SDK简介
安装使用文档逐步操作 准备工作: 1.注册一个GrowingIO账号 2.申请一个域名(注意:不能是ip或host) 登陆gio平台: 1.安装SDK 2.根据项目选择对应的sdk:js,安卓,ios ...
- zabbix部署(1)(lnmp转)
1.lnmp 首先 确保CentOS7上网络配置正确,可以正常访问互联网. 确保已经关闭了iptables. CentOS7上是firewall,关闭命令: 1 2 # systemctl stop ...
- Linux常用系统信息查看命令
[转]http://yulans.cn/linux/linux%E5%B8%B8%E7%94%A8%E7%B3%BB%E7%BB%9F%E4%BF%A1%E6%81%AF%E6%9F%A5%E7%9C ...
- ccf201703-2学生排队
问题描述 体育老师小明要将自己班上的学生按顺序排队.他首先让学生按学号从小到大的顺序排成一排,学号小的排在前面,然后进行多次调整.一次调整小明可能让一位同学出队,向前或者向后移动一段距离后再插入队列. ...
- kettle学习笔记(三)——kettle资源库、运行方式与日志
一.kettle资源库 资源库是用来保存转换任务的,用户通过图形界面创建的的转换任务可以保存在资源库中. 资源库可以使多用户共享转换任务,转换任务在资源库中是以文件夹形式分组管理的,用户可以自定义文件 ...