zoukankan      html  css  js  c++  java
  • LeetCode 725. Split Linked List in Parts

    原题链接在这里:https://leetcode.com/problems/split-linked-list-in-parts/submissions/

    题目:

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

    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 root will be in the range [0, 1000].
    • Each value of a node in the input will be an integer in the range [0, 999].
    • k will be an integer in the range [1, 50].

    题解:

    算出每组有多少个item. 以及多余的个数. 指针跳每组的步数, 如果有多余个数etra, 指针再多跳一步. 在指针后面断开写入res中. extra--.

    Time Complexity: O(n+k). 也有可能k很大, 每组只有一个item.

    Space: O(1).

    AC Java:

     1 /**
     2  * Definition for singly-linked list.
     3  * public class ListNode {
     4  *     int val;
     5  *     ListNode next;
     6  *     ListNode(int x) { val = x; }
     7  * }
     8  */
     9 class Solution {
    10     public ListNode[] splitListToParts(ListNode root, int k) {
    11         ListNode [] res = new ListNode[k];
    12         
    13         int len = getLength(root);
    14         ListNode cur = root;
    15         int group = len/k;
    16         int extra = len%k;
    17         
    18         for(int index = 0; index<k && cur!=null; index++){
    19             res[index] = cur;
    20             
    21             for(int i = 0; i<group-1+(extra>0 ? 1 : 0); i++){
    22                 cur = cur.next;
    23             }
    24             
    25             ListNode mark = cur.next;
    26             cur.next = null;
    27             cur = mark;
    28             extra--;
    29         }
    30         
    31         return res;
    32     }
    33     
    34     private int getLength(ListNode head){
    35         int res = 0;
    36         while(head != null){
    37             res++;
    38             head = head.next;
    39         }
    40         
    41         return res;
    42     }
    43 }
  • 相关阅读:
    如何配置SWRLJess Tab?
    Orz游戏开发框架阅读笔记(一)
    JessDE 在 Eclipse中不能正确安装的问题
    如何在Protege3.4中安装graphviz以便在protege中使用OwlvizTab?
    语义网的学习资源大汇集(转载)
    如何使用Eclipse从Subversion源码服务器下载源码?
    UltraEdit不能对Matlab的M文件进行语法高亮显示问题的解决
    UltraEdit的语法高亮文件网址
    IronPython的致命弱点
    【WPF】用CustomControl打造WPF版的Marquee
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/10965555.html
Copyright © 2011-2022 走看看