• 题目描述:给定一个单链表,写一个函数把它分成k个单链表。分割成的k个单链表中,两两之间长度差不超过1,允许为空。分成的k个链表中,顺序要和原先的保持一致,比如说每个单链表有3个结点,则第一个单链表的结点为输入链表的前三个结点,依次类推。

  • 思路:

  1. 第一次遍历单链表,求出链表的长度length;
  2. 求出平均分成的k个链表中,每个的结点avg,以及还多余的结点rem;
  3. 第二次遍历输入链表,如果达到avg,且rem存在值,则把本次遍历的结果赋值给结果数组;
  1. # Definition for singly-linked list.
  2. # class ListNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.next = None
  6. class Solution(object):
  7. def splitListToParts(self, root, k):
  8. """
  9. :type root: ListNode
  10. :type k: int
  11. :rtype: List[ListNode]
  12. """
  13. ret = [None] * k # 结果
  14. length, move = 0, root
  15. while move:
  16. length += 1
  17. move = move.next
  18. avg, rem = length / k, length % k
  19. move, indexs = root, 0 # 结果数组索引
  20. while move:
  21. tmp = move
  22. pre = ListNode(0) # 当前结点的前一个结点
  23. pre.next = move
  24. num = 0
  25. while num < avg: # 平均分给每个k的结点数目
  26. pre, move = pre.next, move.next
  27. num += 1
  28. if rem: # 平分之后还应该分给前rem个链表每个一个结点
  29. pre, move = pre.next, move.next
  30. rem -= 1
  31. pre.next = None
  32. ret[indexs] = tmp
  33. indexs += 1
  34. return ret

Python解Leetcode: 725. Split Linked List in Parts的更多相关文章

  1. #Leetcode# 725. Split Linked List in Parts

    https://leetcode.com/problems/split-linked-list-in-parts/ Given a (singly) linked list with head nod ...

  2. 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 ...

  3. LeetCode 725. Split Linked List in Parts(分隔链表)

    题意:将原链表分隔成k个链表,要求所有分隔的链表长度差异至多为1,且前面的链表长度必须大于等于后面的链表长度. 分析: (1)首先计算链表总长len (2)根据len得到分隔的链表长度要么为size, ...

  4. [leetcode]725. Split Linked List in Parts链表分块

    思路很简单  按时链表的题做起来很容易犯小错误,思维要缜密 还要多练习啊 做之前最好画算法框图 public ListNode[] splitListToParts(ListNode root, in ...

  5. 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 ...

  6. 【LeetCode】725. Split Linked List in Parts 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【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 ...

  8. 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 ...

  9. 725. Split Linked List in Parts

    ▶ 将一个单链表拆分为长度尽量接近的 k 段 ● 自己的代码,12 ms ■ 记链表长度为 count,目标段数为 k,quo = count / k,mod = count % k,part = m ...

随机推荐

  1. [BJWC2008]王之财宝

    嘟嘟嘟 如果没有限制,而且必须选\(m\)件的话,就是隔板法\(C_{n + m - 1} ^ {m - 1}\)了.现在要选至多\(m\)件,那么就相当于新增一个板儿,分出的新的盒子表示" ...

  2. LibreOJ #110. 乘法逆元

    二次联通门 : LibreOJ #110. 乘法逆元 /* LibreOJ #110. 乘法逆元 求一个数在模意义下的所有逆元 */ #include <cstdio> void read ...

  3. 2D动画如何做出3D体积感

    https://cowlevel.net/article/1959026 <AngerForce>幕后故事 这篇文章是个老坑,最近有时间开始写,也是对之前项目的一个总结和记录吧. 本篇文章 ...

  4. nodejs基础 用http模块 搭建一个简单的web服务器 响应JSON、html

    前端在开发中,大多会想浏览器获取json数据,下面来用nodejs中的http模块搭建一个返回json数据的服务器 var http = require("http"); var ...

  5. python pywin32 安装

    pip install pywin32 参考: https://blog.csdn.net/qq_38161040/article/details/85075158

  6. 使用Excel拼凑SQL语句

       快速将一列多行数据合并到一个单元格            EXCEL如何快速将一列多行数据合并到一个单元格,并加分隔符?这是批量处理由一线业务员统计的数据时的常用方法,尤其是当一列数据是wher ...

  7. Apache Kudu: Hadoop生态系统的新成员实现对快速数据的快速分析

    A new addition to the open source Apache Hadoop ecosystem, Apache Kudu completes Hadoop's storage la ...

  8. 微信小程序之简单记账本开发记录(四)

    昨天搭建了大致界面 今天需要将用到的一系列样式表配置出来并检查错误

  9. 学习GeoServer遇到的问题及答案

    简介:本文的记录学习GeoServer遇到的问题,如果已有答案将会附上. 1.GeoServer介绍?,功能? GeoServer 是 OpenGIS Web 服务器规范的 J2EE 实现,利用 Ge ...

  10. radio自带回显和默认选中

    <input type="radio" name="state" <c:if test="${empty model.state || m ...