Given a (singly) linked list with head node root
, write a function to split the linked list into k
consecutive linked list "parts".
The length of each part should be as equal as possible: no two parts should have a size differing by more than 1. This may lead to some parts being null.
The parts should be in order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal parts occurring later.
Return a List of ListNode's representing the linked list parts that are formed.
Examples1->2->3->4, k = 5 // 5 equal parts[ [1], [2],[3],[4],null ]Example 1:
Input: root = [1, 2, 3], k = 5 Output: [[1],[2],[3],[],[]] Explanation: The input and each element of the output are ListNodes, not arrays. For example, the input root has root.val = 1, root.next.val = 2, \root.next.next.val = 3, and root.next.next.next = null. The first element output[0] has output[0].val = 1, output[0].next = null. The last element output[4] is null, but it's string representation as a ListNode is [].
Example 2:
Input: root = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3 Output: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]] Explanation: The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts.
Note:
root
will be in the range [0, 1000]
.Each value of a node in the input will be an integer in the range [0, 999]
.k
will be an integer in the range [1, 50]
.思路:
要求对链表进行分割,所以需要直到链表的总元素数len。这就要遍历链表求出。
然后根据len和要求分割的段数k,求出平均分的情况下每段的元素数len / k,以及第k部分有多少个元素。将这几个元素分配到平均分配到最前面的几个分段中即可。
注意:每段结尾的下一个指针指向NULL,需修改,len%k部分即需要比后面长1的部分个数
代码1:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<ListNode*> splitListToParts(ListNode* root, int k) {
vector<ListNode*> result;
int len=0,graterpart;
for (ListNode* node = root; node; node = node->next){len++;}
int sublen=len/k;
if(sublen<1){
sublen=1;
graterpart=k;
}
else{graterpart=k-len%k;}
ListNode* node=root, *pre = nullptr;
while(k){
result.push_back(node);
for(int i=1;i<=sublen+(k>graterpart);i++){
if(!node) {break;}
else if(i<sublen+(k>graterpart)){node = node->next;}
else{
pre=node;
node = node->next;
pre->next=nullptr;
}
}
k--;
}
return result;
}
};
代码2:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<ListNode*> splitListToParts(ListNode* root, int k) {
vector<ListNode*> res(k,NULL);
int cnt=0,i=0,no,ext;
ListNode* temp=root;
while(temp!=NULL){
cnt++;
temp = temp->next;
}
no=cnt/k;
ext=cnt%k;
temp=root;
while(temp!=NULL){
res[i]=temp;
i++;
int loop=no;
if(ext>0){loop++;}
ext--;
while(temp!=NULL && loop!=0){
loop--;
if(loop==0){
ListNode* temp2=temp->next;
temp->next = NULL;
temp=temp2;
}
else{temp = temp->next;}
}
}
return res;
}
};
代码3:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<ListNode*> splitListToParts(ListNode* root, int k) {
if (k == 1) {
return vector<ListNode*>({root});
}
vector<ListNode*> res(k, NULL);
int total_len = getLen(root);
int per_len = total_len / k;
int remainder = total_len % k;
int idx = 0, tmp = 0;
while (idx < k) {
tmp = per_len + (idx < remainder ? 1 : 0);
if (tmp == 0) {
res[idx++] = NULL;
continue;
}
res[idx++] = root;
while (tmp != 1) {
root = root->next;
--tmp;
}
ListNode *next = root->next;
root->next = NULL;
root = next;
}
return res;
}
int getLen(ListNode *root) {
int len = 0;
while (root != NULL) {
++len;
root = root->next;
}
return len;
}
};