zoukankan      html  css  js  c++  java
  • 725. Split Linked List in Parts把链表分成长度不超过1的若干部分

    [抄题]:

    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, 
    oot.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 [].

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    不知道怎么平均分配,以为是dp,结果除一下就可以了

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    [一句话思路]:

    k组中,每一组开个头,然后n+r位用两个节点往后贴

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    1. 往后复制链表需要两个节点,一个node,一个prev。
    2. 数linkedlist的长度需要一个辅助节点node。

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    1. 往后复制链表需要两个节点,一个node,一个prev。

    [复杂度]:Time complexity: O(n) Space complexity: O(n)

    [算法思想:迭代/递归/分治/贪心]:

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

     [是否头一次写此类driver funcion的代码] :

     [潜台词] :

    //root = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3
    //Output: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
    
    class Solution {
        public ListNode[] splitListToParts(ListNode root, int k) {
            //initialization
            ListNode[] result = new ListNode[k];
            
            //corner case
            if (root == null || k <= 0) return result;
            
            //for loop for i and r: add each node for n times
            int len = 0;
            for (ListNode node = root; node != null; node = node.next) {
                len++;
            }
            
            int n = len / k; int r = len % k;
            ListNode node = root; ListNode prev = null;
            for (int i = 0; i < k && node != null; i ++, r--) {
                //for loop for j: add a r if necessary
                result[i] = node;
                for (int j = 0; j < n + (r > 0 ? 1 : 0); j++) {
                    prev = node;
                    node = node.next;
                }
                prev.next = null;
            }
            //return 
            return result;
        }
    }
    View Code
  • 相关阅读:
    教你一招Linux下文本比对方法
    Linux下find与exec的联手干大事
    Linux下Shell日期的格式,你知道几种?
    Linux下Python3.6的安装及避坑指南
    多线程中使用CompletableFuture
    ElasticSearch7.6.2中语法使用(更新中)
    ElasticSearch7.6.2使用_update_by_query语法
    ElasticSearch7.6.2使用_delete_by_query产生版本冲突问题
    filebeat7.6.2进程运行一段时间后自动退出问题解决
    把本地项目提交到gitLab
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9424475.html
Copyright © 2011-2022 走看看