zoukankan      html  css  js  c++  java
  • 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 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].

     attention:

    a = rootlen / k

    b = rootlen % k 

    a is the basic elements that will appear in every k array.

    b is the more elements that pre-b array need to append.

    eg.

    [1,2,3,4,5,6,7]

    rootlen = 7, k  = 4

    a = 1

    b = 2

    so 

    loop 1, append a(1) elements from root.

    [[1] [] [] []] , b > 0   => [[1,2],[],[],[]], b = b-1 (1)

    loop2 append a(1) elements from root.

    [[1,2],[3],[],[]], b>0 => [[1,2],[3,4],[],[]] b = b-1(0)

    loop 3 ....

    Runtime: 3 ms, faster than 98.84% of Java online submissions for Split Linked List in Parts.

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
      public ListNode[] splitListToParts(ListNode root, int k) {
        int rootlen = 0;
        ListNode[] ln = new ListNode[k];
        ListNode tmp = root, tmpnext;
        while(tmp != null){
          tmp = tmp.next;
          rootlen++;
    
        }
        if(rootlen == 0) return ln;
        int a = rootlen % k;
        int b = rootlen / k;
        int cnt = 0;
        for(int i=0; i<k; i++) {
          if(root == null) continue;
          tmp = root;
          for(int j=0; j<b-1; j++){
            tmp = tmp.next;
          }
          if(a != 0 && b != 0) {
            tmp = tmp.next;
            a--;
          }
          tmpnext = tmp.next;
          tmp.next = null;
          ln[cnt++] = root;
          root = tmpnext;
        }
        return ln;
      }
    }
  • 相关阅读:
    linux安装mongo-c-driver
    DPDK在虚拟机上运行时,报错: Ethdev port_id=0 requested Rx offloads 0xe doesn't match Rx offloads capabilities 0x82a1d in rte_eth_dev_configure()
    用python写xml文件
    openvas在centos中扫描单项的python实现
    ARP协议的报文格式
    python装饰器使用
    openvas漏洞扫描:使用openvas时扫描漏洞时,报告中显示的数据与数据库数据不同
    单链表实现一元多项式乘法与加法运算(C语言)
    Java学习笔记DayN Java高级特性概况
    Java学习笔记Day5 集合
  • 原文地址:https://www.cnblogs.com/ethanhong/p/10260887.html
Copyright © 2011-2022 走看看