-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
41 lines (35 loc) · 894 Bytes
/
Solution.cs
File metadata and controls
41 lines (35 loc) · 894 Bytes
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
public class Solution
{
public ListNode[] SplitListToParts(ListNode head, int k)
{
var res = new ListNode[k];
if (head is null || k == 0) return res;
int headLength = 0;
var tmpHead = head;
while (tmpHead is not null)
{
headLength++;
tmpHead = tmpHead.next;
}
int div = headLength / k;
int mod = headLength % k;
for (int i = 0; i < k; i++)
{
res[i] = head;
int pos = 0;
int size = div + (mod > 0 ? 1 : 0);
while (head is not null && pos < size)
{
pos++;
tmpHead = head;
head = head.next;
}
if (tmpHead is not null)
{
tmpHead.next = null;
}
mod--;
}
return res;
}
}