题目描述
给定一个头结点为 root 的链表, 编写一个函数以将链表分隔为 k 个连续的部分。
每部分的长度应该尽可能的相等: 任意两部分的长度差距不能超过 1,也就是说可能有些部分为 null。
这k个部分应该按照在链表中出现的顺序进行输出,并且排在前面的部分的长度应该大于或等于后面的长度。
返回一个符合上述规则的链表的列表。
举例: 1->2->3->4, k = 5 // 5 结果 [ [1], [2], [3], [4], null ]
示例 1:
输入:
root = [1, 2, 3], k = 5
输出: [[1],[2],[3],[],[]]
解释:
输入输出各部分都应该是链表,而不是数组。
例如, 输入的结点 root 的 val= 1, root.next.val = 2, \root.next.next.val = 3, 且 root.next.next.next = null。
第一个输出 output[0] 是 output[0].val = 1, output[0].next = null。
最后一个元素 output[4] 为 null, 它代表了最后一个部分为空链表。
示例 2:
输入:
root = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3
输出: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
解释:
输入被分成了几个连续的部分,并且每部分的长度相差不超过1.前面部分的长度大于等于后面部分的长度。
提示:
root 的长度范围: [0, 1000].
输入的每个节点的大小范围:[0, 999].
k 的取值范围: [1, 50].
方法思路
我的错误代码:
我的思路与方法一类似,但是没有想到新建链表辅助,想得太复杂了。
要善于新建链表辅助,有时候会让问题变得很简单。
class Solution {
public ListNode[] splitListToParts(ListNode root, int k) {
ListNode[] splitList = new ListNode[k];
int length = 0, remainder = 0, quotient = 0;
ListNode cur = root, count = root;
//链表长度length
while(count != null){
count = count.next;
length++;
}
quotient = length / k;//商,即链表数组中每个链表元素的最小长度
remainder = length % k;//余数,前remainder个链表会比后面的链表多一个节点
if(quotient == 0){
for(int i = 0; remainder > 0; remainder--, i++){
splitList[i] = cur;
if(splitList[i] == null) break;
splitList[i].next = null;
cur = cur.next;
}
}else{
int bits = 0;
for(int i = 0; i < k; i++){
bits = quotient;
splitList[i] = cur;
while(bits > 1){
cur = cur.next;
bits--;
}
if(remainder > 0){
cur = cur.next;
remainder--;
}
if(cur == null) break;
ListNode curNext = cur.next;
cur.next = null;
cur = curNext;
}
}
return splitList;
}
}
Approach1: Create New Lists
If there are NNN nodes in the linked list root, then there are N/k items in each part, plus the first N%k parts have an extra item. We can count N with a simple loop.
Now for each part, we have calculated how many nodes that part will have: width + (i < remainder ? 1 : 0). We create a new list and write the part to that list.
Our solution showcases constructs of the form a = b = c. Note that this syntax behaves differently for different languages.
class Solution {
public ListNode[] splitListToParts(ListNode root, int k) {
ListNode cur = root;
int N = 0;
while (cur != null) {
cur = cur.next;
N++;
}
int width = N / k, rem = N % k;
ListNode[] ans = new ListNode[k];
cur = 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.next = new ListNode(cur.val);
write = write.next;
if (cur != null) cur = cur.next;
}
ans[i] = head.next;
}
return ans;
}
}
Complexity Analysis
Time Complexity: O(N+k), where N is the number of nodes in the given list.
If k is large, it could still require creating many new empty lists.
Space Complexity: O(max(N,k)), the space used in writing the answer.
Approach #2: Split Input List [Accepted]
class Solution {
//Runtime: 1 ms, faster than 100.00%
//Memory Usage: 38.4 MB, less than 5.36%
public ListNode[] splitListToParts(ListNode root, int k) {
ListNode cur = root;
int N = 0;
while (cur != null) {
cur = cur.next;
N++;
}
int width = N / k, rem = N % k;
ListNode[] ans = new ListNode[k];
cur = root;
for (int i = 0; i < k; ++i) {
ListNode head = cur;
for (int j = 0; j < width + (i < rem ? 1 : 0) - 1; ++j) {
if (cur != null) cur = cur.next;
}
if (cur != null) {
ListNode prev = cur;
cur = cur.next;
prev.next = null;
}
ans[i] = head;
}
return ans;
}
}