#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 ...
随机推荐
- Spark项目之电商用户行为分析大数据平台之(十)IDEA项目搭建及工具类介绍
一.创建Maven项目 创建项目,名称为LogAnalysis 二.常用工具类 2.1 配置管理组建 ConfigurationManager.java import java.io.InputStr ...
- 清北学堂寒假集训DAY1
第一天,上午讲了些基本的技巧和简单算法,主要就是适应这里. 中午跑到食堂吃了顿“饭”(我并没有挖苦233333),然后回宿舍休息休息 因为 迎接我们的是模拟啊啊啊啊啊阿 下午题一发下来,并没有想象中的 ...
- MVC 全局拦截aciton
上篇:MVC 拦截指定的action 有时,我们需要对所有aciton在执行前/后做一些(预)处理,如何实现呢? 1.定义一个action筛选类.继承至System.Web.Mvc.IActionFi ...
- 开关电源五种PWM反馈控制模式
开关电源五种PWM反馈控制模式 来源:--作者:--浏览:178时间:2016-08-10 14:18 关键词: 1 引言 PWM开关稳压或稳流电源基本工作原理就是在输入电压变化.内部参数变化.外接负 ...
- linux中原子操作实现方式
原子操作提供了指令原子执行,中间没有中断.就像原子被认为是不可分割颗粒一样,原子操作(atomic operation)是不可分割的操作. 如下面简单的例子: Thread 1 ...
- ccf201703-1分蛋糕
问题描述 小明今天生日,他有n块蛋糕要分给朋友们吃,这n块蛋糕(编号为1到n)的重量分别为a1, a2, …, an.小明想分给每个朋友至少重量为k的蛋糕.小明的朋友们已经排好队准备领蛋糕,对于每个朋 ...
- 20155223 Exp7 网络欺诈防范
20155223 Exp7 网络欺诈防范 基础问题回答 通常在什么场景下容易受到DNS spoof攻击? 无设防或防护力特别弟弟低的公共局域网,或者是在同一个局域网下. 在日常生活工作中如何防范以上两 ...
- 20155325 Exp2 后门原理与实践
基础问答 例举你能想到的一个后门进入到你系统中的可能方式? 乱点链接 学电脑小白不正确配置电脑 下载非官网软件 例举你知道的后门如何启动起来(win及linux)的方式? 软件:ncat socat ...
- [FQ]Tor + Chrome + PAC 尝试 FQ
记录一次比较成功的FQ经历 1.从Tor官网下载最新的Tor browser,速度较慢可以从文末给出的链接中下载. 2.安装Tor browser. 3. Tor网络设置 3.1 那个描述与你的情况最 ...
- mfc 动态为控件添加事件2
重载窗口过程 为动态控件绑定事件 一.重载窗口过程处理函数 CWnd::WindowProc virtual LRESULT WindowProc( UINT message, WPARAM wPar ...