Skip to content

Commit 13cb0fd

Browse files
refactor 725
1 parent 2b6d01b commit 13cb0fd

File tree

2 files changed

+71
-9
lines changed

2 files changed

+71
-9
lines changed

src/main/java/com/fishercoder/solutions/_725.java

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ public static class Solution1 {
77
/**
88
* My very original solution, but verbose.
99
*/
10-
public ListNode[] splitListToParts(ListNode root, int k) {
11-
int len = getLength(root);
10+
public ListNode[] splitListToParts(ListNode head, int k) {
11+
int len = getLength(head);
1212
int aveSize = len / k;
1313
int extra = len % k;
1414
ListNode[] result = new ListNode[k];
1515
for (int i = 0; i < k; i++) {
16-
result[i] = root;
16+
result[i] = head;
1717
int aveSizeTmp = aveSize;
1818
aveSizeTmp += extra-- > 0 ? 1 : 0;
1919
int aveSizeTmp2 = aveSizeTmp;
2020
while (aveSizeTmp-- > 0) {
21-
root = root.next;
21+
head = head.next;
2222
}
2323
if (result[i] != null) {
2424
ListNode tmp = result[i];
@@ -46,17 +46,17 @@ public static class Solution2 {
4646
/**
4747
* More concise version
4848
*/
49-
public ListNode[] splitListToParts(ListNode root, int k) {
50-
int len = getLength(root);
49+
public ListNode[] splitListToParts(ListNode head, int k) {
50+
int len = getLength(head);
5151
int aveSize = len / k;
5252
int extra = len % k;
5353
ListNode[] result = new ListNode[k];
5454
ListNode prev = null;
5555
for (int i = 0; i < k; i++, extra--) {
56-
result[i] = root;
56+
result[i] = head;
5757
for (int j = 0; j < aveSize + (extra > 0 ? 1 : 0); j++) {
58-
prev = root;
59-
root = root.next;
58+
prev = head;
59+
head = head.next;
6060
}
6161
if (prev != null) {
6262
prev.next = null;
@@ -75,4 +75,44 @@ private int getLength(ListNode root) {
7575
return len;
7676
}
7777
}
78+
79+
public static class Solution3 {
80+
/**
81+
* My original solution on 9/29/2021.
82+
*/
83+
public ListNode[] splitListToParts(ListNode head, int k) {
84+
ListNode[] ans = new ListNode[k];
85+
int size = 0;
86+
ListNode tmp = head;
87+
while (tmp != null) {
88+
tmp = tmp.next;
89+
size++;
90+
}
91+
int minSize = size / k;
92+
int remainder = size % k;
93+
int i = 0;
94+
if (head == null) {
95+
while (i < k) {
96+
ans[i++] = null;
97+
}
98+
}
99+
while (i < k) {
100+
ListNode node = head;
101+
tmp = node;
102+
int len = minSize;
103+
if (remainder > 0) {
104+
remainder--;
105+
len++;
106+
}
107+
while (len-- > 1) {
108+
tmp = tmp.next;
109+
}
110+
head = tmp.next;
111+
tmp.next = null;
112+
ans[i] = node;
113+
i++;
114+
}
115+
return ans;
116+
}
117+
}
78118
}

src/test/java/com/fishercoder/_725Test.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
public class _725Test {
1010
private static _725.Solution1 solution1;
1111
private static _725.Solution2 solution2;
12+
private static _725.Solution3 solution3;
1213
private static ListNode root;
1314
private static int k;
1415
private static ListNode[] actual;
@@ -17,6 +18,7 @@ public class _725Test {
1718
public static void setup() {
1819
solution1 = new _725.Solution1();
1920
solution2 = new _725.Solution2();
21+
solution3 = new _725.Solution3();
2022
}
2123

2224
@Test
@@ -59,4 +61,24 @@ public void test4() {
5961
}
6062
}
6163

64+
@Test
65+
public void test5() {
66+
root = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3});
67+
k = 5;
68+
actual = solution3.splitListToParts(root, k);
69+
for (ListNode head : actual) {
70+
LinkedListUtils.printList(head);
71+
}
72+
}
73+
74+
@Test
75+
public void test6() {
76+
root = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
77+
k = 3;
78+
actual = solution3.splitListToParts(root, k);
79+
for (ListNode head : actual) {
80+
LinkedListUtils.printList(head);
81+
}
82+
}
83+
6284
}

0 commit comments

Comments
 (0)