-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ725SplitLinkedList.java
50 lines (43 loc) · 1.59 KB
/
Q725SplitLinkedList.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
/*
@b-knd (jingru) on 03 August 2022 09:49:00
*/
class Solution {
public ListNode[] splitListToParts(ListNode head, int k) {
//traverse linked list to find length
int len = 0;
ListNode temp = head;
while(temp != null){
len++;
temp = temp.next;
}
//if len modulo k has remainder, means the first x elements (where x = remainder) will have extra one element than min (minimum element in each linked list)
int remainder = len % k;
int min = len / k;
ListNode[] res = new ListNode[k];
Arrays.fill(res, null);
temp = head; ListNode temp2 = null;
int index = 0;
while(temp != null){
//initiate size to min or min+1 if remainder > 0 (to distribute all element without leftover)
int size = min;
if(remainder > 0){
size++;
remainder--;
}
res[index] = temp;
for(int i = 1; i < size; i++){
temp = temp.next;
}
//continue with the rest of the nodes, ending current list by setting null as next, increment index
if(temp != null){
temp2 = temp.next;
temp.next = null;
temp = temp2;
}
index++;
}
return res;
}
}
//Runtime: 0 ms, faster than 100.00% of Java online submissions for Split Linked List in Parts.
//Memory Usage: 42.2 MB, less than 96.14% of Java online submissions for Split Linked List in Parts.