-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
725.java
65 lines (65 loc) · 1.94 KB
/
725.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
__________________________________________________________________________________________________
sample 0 ms submission
/**
* 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) {
ListNode curr = root;
int n= 0;
while(curr!=null){
curr = curr.next;
n++;
}
int width = n/k, rem = n%k;
ListNode[] ans = new ListNode[k];
curr = root;
for(int i=0; i<k; i++){
ListNode head = new ListNode(0), write = head;
for(int j=0; j<width+(i<rem?1:0); j++){
write = write.next = new ListNode(curr.val);
if(curr!=null)
curr = curr.next;
}
ans[i] = head.next;
}
return ans;
}
}
__________________________________________________________________________________________________
sample 37072 kb submission
/**
* 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 len = 0;
for (ListNode cur = root; cur != null; cur = cur.next) len++;
int base = len / k;
int remainder = len % k;
ListNode[] result = new ListNode[k];
for (int i = 0; i < k; i++) {
result[i] = root;
if(root == null) continue;
int step = remainder-- > 0 ? base + 1 : base;
for (int j = 0; j < step - 1; j++) {
root = root.next;
}
ListNode tmp = root.next;
root.next = null;
root = tmp;
}
return result;
}
}
__________________________________________________________________________________________________