package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/split-linked-list-in-parts/
 */
public class Problem725 {
    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }

        @Override
        public String toString() {
            return "" + val;
        }
    }

    public ListNode[] splitListToParts(ListNode root, int k) {
        int size = 0;
        for (ListNode node = root; node != null; node = node.next) {
            size++;
        }
        int division = size / k;
        int remainder = size % k;
        ListNode[] result = new ListNode[k];
        int i = 0;
        int d = division;
        ListNode previous = null;
        ListNode current = root;
        while (current != null) {
            if (i == 0) {
                result[i++] = current;
            } else if (d <= 0) {
                if (remainder > 0) {
                    previous = current;
                    current = current.next;
                    remainder--;
                }
                previous.next = null;
                result[i++] = current;
                d = division;
            }
            d--;
            previous = current;
            current = current.next;
        }
        return result;
    }

    private static ListNode build(int... nums) {
        ListNode head = null;
        ListNode next = null;
        for (int i = 0; i < nums.length; i++) {
            if (i == 0) {
                head = new ListNode(nums[i]);
                next = head;
            } else {
                next.next = new ListNode(nums[i]);
                next = next.next;
            }
        }
        return head;
    }

    public static void main(String[] args) {
        Problem725 prob = new Problem725();
        System.out.println(Arrays.toString(prob.splitListToParts(build(1, 2, 3), 5))); // [[1],[2],[3],[],[]]
//        System.out.println(Arrays.toString(prob.splitListToParts(build(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 3))); // [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
//        System.out.println(Arrays.toString(prob.splitListToParts(build(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 7))); // [[1,2],[3,4],[5,6],[7],[8],[9],[10]]
    }
}

@@ -16,34 +16,8 @@
 e;%0A%0A
-import java.util.Arrays;%0A%0A
 /**%0A
@@ -243,101 +243,8 @@
    %7D
-%0A%0A        @Override%0A        public String toString() %7B%0A            return %22%22 + val;%0A        %7D
 %0A   
@@ -820,24 +820,62 @@
 nder %3E 0) %7B%0A
+                    if (d == 0) %7B%0A    
             
@@ -894,32 +894,36 @@
 ious = current;%0A
+    
                 
@@ -942,32 +942,54 @@
 = current.next;%0A
+                    %7D%0A
                 
@@ -1649,32 +1649,32 @@
     %7D%0A        %7D%0A
+
         return h
@@ -1687,516 +1687,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem725 prob = new Problem725();%0A        System.out.println(Arrays.toString(prob.splitListToParts(build(1, 2, 3), 5))); // %5B%5B1%5D,%5B2%5D,%5B3%5D,%5B%5D,%5B%5D%5D%0A//        System.out.println(Arrays.toString(prob.splitListToParts(build(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 3))); // %5B%5B1, 2, 3, 4%5D, %5B5, 6, 7%5D, %5B8, 9, 10%5D%5D%0A//        System.out.println(Arrays.toString(prob.splitListToParts(build(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 7))); // %5B%5B1,2%5D,%5B3,4%5D,%5B5,6%5D,%5B7%5D,%5B8%5D,%5B9%5D,%5B10%5D%5D%0A    %7D
 %0A%7D%0A

Solve problem 725