-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetcode725.java
57 lines (47 loc) · 1.31 KB
/
Leetcode725.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
class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
class Solution{
public ListNode[] splitListToParts(ListNode head, int k){
ListNode[] and=new ListNode[k];
// Size of LL
int size=0;
ListNode current=head;
while(current!=null){
size++;
current=current.next;
}
int splitSize= size/k;
int numRemainingParts=size%k;
current=head;
for(int i=0;i<k;i++){
ListNode newPart=new ListNode(0);
ListNode tail=newPart;
int currentSize=splitSize;
if(numRemainingParts>0){
numRemainingParts--;
currentSize++;
}
int j=0;
while(j<currentSize){
tail.next=new ListNode(current.val);
tail=tail.next;
j++;
current=current.next;
}
ans[i]=newPart.next;
}
return ans;
}
}
public class Leetcode725{
public static void main(String args[]){
int [] head={1,2,3};
int k=5;
splitListToParts(head,k);
}
}