Leetcode:725. 分隔链表
class Solution {
public ListNode[] splitListToParts(ListNode head, int k) {
int len = 0;
ListNode temp = head;
while(temp != null){
len++;
temp = temp.next;
}
ListNode cur = head;
int size = len / k;
int mode = len % k;
ListNode[] res = new ListNode[k];
for(int i = 0;i < k;i++){
res[i] = cur;
int count = mode-- > 0 ? 1 : 0;
for(int j = 0;j < size + count - 1;j++){
if(cur != null)
cur = cur.next;
}
if(cur != null){
ListNode curTemp = cur.next;
cur.next = null;
cur = curTemp;
}
}
return res;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。