Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for singly-linked list.
- * struct ListNode {
- * int val;
- * ListNode *next;
- * ListNode() : val(0), next(nullptr) {}
- * ListNode(int x) : val(x), next(nullptr) {}
- * ListNode(int x, ListNode *next) : val(x), next(next) {}
- * };
- */
- class Solution {
- public:
- vector<ListNode*> splitListToParts(ListNode* root, int k) {
- ListNode *node, *prev;
- int len, n, r;
- len = 0;
- node = root;
- while(node){
- len++;
- node = node->next;
- }
- n = len/k; r = len%k;
- vector<ListNode*> parts(k, NULL);
- node = root;
- for(int i=0; i<k && node; i++,r--){
- parts[i] = node;
- for(int j=0; j<n+(r>0); j++){
- prev = node;
- node = node->next;
- }
- prev->next = NULL;
- }
- return parts;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement