Python解Leetcode: 725. Split Linked List in Parts
题目描述:给定一个单链表,写一个函数把它分成k个单链表。分割成的k个单链表中,两两之间长度差不超过1,允许为空。分成的k个链表中,顺序要和原先的保持一致,比如说每个单链表有3个结点,则第一个单链表的结点为输入链表的前三个结点,依次类推。
思路:
- 第一次遍历单链表,求出链表的长度length;
- 求出平均分成的k个链表中,每个的结点avg,以及还多余的结点rem;
- 第二次遍历输入链表,如果达到avg,且rem存在值,则把本次遍历的结果赋值给结果数组;
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def splitListToParts(self, root, k):
"""
:type root: ListNode
:type k: int
:rtype: List[ListNode]
"""
ret = [None] * k # 结果
length, move = 0, root
while move:
length += 1
move = move.next
avg, rem = length / k, length % k
move, indexs = root, 0 # 结果数组索引
while move:
tmp = move
pre = ListNode(0) # 当前结点的前一个结点
pre.next = move
num = 0
while num < avg: # 平均分给每个k的结点数目
pre, move = pre.next, move.next
num += 1
if rem: # 平分之后还应该分给前rem个链表每个一个结点
pre, move = pre.next, move.next
rem -= 1
pre.next = None
ret[indexs] = tmp
indexs += 1
return ret
Python解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 (分裂链表)
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 ...
- 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 ...
随机推荐
- [BJWC2008]王之财宝
嘟嘟嘟 如果没有限制,而且必须选\(m\)件的话,就是隔板法\(C_{n + m - 1} ^ {m - 1}\)了.现在要选至多\(m\)件,那么就相当于新增一个板儿,分出的新的盒子表示" ...
- LibreOJ #110. 乘法逆元
二次联通门 : LibreOJ #110. 乘法逆元 /* LibreOJ #110. 乘法逆元 求一个数在模意义下的所有逆元 */ #include <cstdio> void read ...
- 2D动画如何做出3D体积感
https://cowlevel.net/article/1959026 <AngerForce>幕后故事 这篇文章是个老坑,最近有时间开始写,也是对之前项目的一个总结和记录吧. 本篇文章 ...
- nodejs基础 用http模块 搭建一个简单的web服务器 响应JSON、html
前端在开发中,大多会想浏览器获取json数据,下面来用nodejs中的http模块搭建一个返回json数据的服务器 var http = require("http"); var ...
- python pywin32 安装
pip install pywin32 参考: https://blog.csdn.net/qq_38161040/article/details/85075158
- 使用Excel拼凑SQL语句
快速将一列多行数据合并到一个单元格 EXCEL如何快速将一列多行数据合并到一个单元格,并加分隔符?这是批量处理由一线业务员统计的数据时的常用方法,尤其是当一列数据是wher ...
- Apache Kudu: Hadoop生态系统的新成员实现对快速数据的快速分析
A new addition to the open source Apache Hadoop ecosystem, Apache Kudu completes Hadoop's storage la ...
- 微信小程序之简单记账本开发记录(四)
昨天搭建了大致界面 今天需要将用到的一系列样式表配置出来并检查错误
- 学习GeoServer遇到的问题及答案
简介:本文的记录学习GeoServer遇到的问题,如果已有答案将会附上. 1.GeoServer介绍?,功能? GeoServer 是 OpenGIS Web 服务器规范的 J2EE 实现,利用 Ge ...
- radio自带回显和默认选中
<input type="radio" name="state" <c:if test="${empty model.state || m ...