Skip to content

Commit b8f24b7

Browse files
committed
725. Split Linked List in Parts
1 parent ef239f2 commit b8f24b7

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//https://leetcode.com/problems/split-linked-list-in-parts/
2+
3+
class Solution {
4+
public:
5+
vector<ListNode*> splitListToParts(ListNode* head, int k) {
6+
int size = 0;
7+
ListNode* temp = head;
8+
while(temp){
9+
size++;
10+
temp = temp->next;
11+
}
12+
int base_size = size/k;
13+
int base_inc = size%k;
14+
int i = 1;
15+
temp = head;
16+
vector<ListNode*> result;
17+
while(i<=k){
18+
int ll_size = base_size;
19+
if(i<=base_inc) ll_size++;
20+
ListNode* t = temp;
21+
while((ll_size-1)>0){
22+
temp = temp->next;
23+
ll_size--;
24+
}
25+
ListNode* tail = temp;
26+
if(temp) temp = temp->next;
27+
if(tail) tail->next = NULL;
28+
result.push_back(t);
29+
i++;
30+
}
31+
return result;
32+
}
33+
};

0 commit comments

Comments
 (0)