码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • leetcode - 725. Split Linked List in Parts


    Description

    Given the head of a singly linked list and an integer k, 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 one. This may lead to some parts being null.

    The parts should be in the order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal to parts occurring later.

    Return an array of the k parts.

    Example 1:
    在这里插入图片描述

    Input: head = [1,2,3], k = 5
    Output: [[1],[2],[3],[],[]]
    Explanation:
    The first element output[0] has output[0].val = 1, output[0].next = null.
    The last element output[4] is null, but its string representation as a ListNode is [].
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Example 2:
    在这里插入图片描述

    Input: head = [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.
    
    • 1
    • 2
    • 3
    • 4

    Constraints:

    The number of nodes in the list is in the range [0, 1000].
    0 <= Node.val <= 1000
    1 <= k <= 50
    
    • 1
    • 2
    • 3

    Solution

    For the first few segments, the length should be k + 1, and all the rest should be k.

    Time complexity: o ( n ) o(n) o(n)
    Space complexity: o ( 1 ) o(1) o(1)

    Code

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, val=0, next=None):
    #         self.val = val
    #         self.next = next
    class Solution:
        def splitListToParts(self, head: Optional[ListNode], k: int) -> List[Optional[ListNode]]:
            linked_len = 0
            p = head
            while p:
                linked_len += 1
                p = p.next
            length = linked_len // k
            first_parts = linked_len % k
            res = []
            p = head
            cur_len = 0
            while p:
                if cur_len == 0:
                    res.append(p)
                cur_len += 1            
                if first_parts > 0 and cur_len == length + 1:
                    cur_len = 0
                    first_parts -= 1
                    pn = p.next
                    p.next = None
                    p = pn
                elif first_parts == 0 and cur_len == length:
                    cur_len = 0
                    pn = p.next
                    p.next = None
                    p = pn
                else:
                    p = p.next
            return res + [None] * (k - len(res))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
  • 相关阅读:
    python 将字节字符串转换成十六进制字符串
    深度之眼(二)——矩阵及其基本运算
    数据结构之顺序表
    数据结构与算法
    Windows 11 22H2 (2022 年更新) 发布,简体中文版、英文版下载
    Docker搭建Sentinel 控制台环境搭建及使用介绍
    基于R语言的贝叶斯网络模型、现代贝叶斯统计学方法
    获取Flink作业在HDFS上保存的最新的savepoint文件路径
    八大排序代码——总结
    采集分析仪设计原理图:437-带触摸显示的10路5Msps@18bit采集分析仪
  • 原文地址:https://blog.csdn.net/sinat_41679123/article/details/132798703
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号