|
2 | 2 |
|
3 | 3 | import com.fishercoder.common.classes.ListNode;
|
4 | 4 |
|
5 |
| -/** |
6 |
| - * 725. Split Linked List in Parts |
7 |
| - * |
8 |
| - * Given a (singly) linked list with head node root, |
9 |
| - * write a function to split the linked list into k consecutive linked list "parts". |
10 |
| - * The length of each part should be as equal as possible: |
11 |
| - * no two parts should have a size differing by more than 1. This may lead to some parts being null. |
12 |
| - * The parts should be in order of occurrence in the input list, |
13 |
| - * and parts occurring earlier should always have a size greater than or equal parts occurring later. |
14 |
| - * Return a List of ListNode's representing the linked list parts that are formed. |
15 |
| -
|
16 |
| - Examples 1->2->3->4, k = 5 // 5 equal parts [ [1], [2], [3], [4], null ] |
17 |
| -
|
18 |
| - Example 1: |
19 |
| - Input: |
20 |
| - root = [1, 2, 3], k = 5 |
21 |
| - Output: [[1],[2],[3],[],[]] |
22 |
| - Explanation: |
23 |
| - The input and each element of the output are ListNodes, not arrays. |
24 |
| - For example, the input root has root.val = 1, root.next.val = 2, \root.next.next.val = 3, and root.next.next.next = null. |
25 |
| - The first element output[0] has output[0].val = 1, output[0].next = null. |
26 |
| - The last element output[4] is null, but it's string representation as a ListNode is []. |
27 |
| -
|
28 |
| - Example 2: |
29 |
| - Input: |
30 |
| - root = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3 |
31 |
| - Output: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]] |
32 |
| - Explanation: |
33 |
| - 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. |
34 |
| -
|
35 |
| - Note: |
36 |
| - The length of root will be in the range [0, 1000]. |
37 |
| - Each value of a node in the input will be an integer in the range [0, 999]. |
38 |
| - k will be an integer in the range [1, 50]. |
39 |
| -
|
40 |
| - */ |
41 | 5 | public class _725 {
|
42 | 6 | public static class Solution1 {
|
43 |
| - /**My very original solution, but verbose.*/ |
| 7 | + /** |
| 8 | + * My very original solution, but verbose. |
| 9 | + */ |
44 | 10 | public ListNode[] splitListToParts(ListNode root, int k) {
|
45 | 11 | int len = getLength(root);
|
46 | 12 | int aveSize = len / k;
|
@@ -77,7 +43,9 @@ private int getLength(ListNode root) {
|
77 | 43 | }
|
78 | 44 |
|
79 | 45 | public static class Solution2 {
|
80 |
| - /**More concise version*/ |
| 46 | + /** |
| 47 | + * More concise version |
| 48 | + */ |
81 | 49 | public ListNode[] splitListToParts(ListNode root, int k) {
|
82 | 50 | int len = getLength(root);
|
83 | 51 | int aveSize = len / k;
|
|
0 commit comments