We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ef239f2 commit b8f24b7Copy full SHA for b8f24b7
Data Structures/Linked List/SplitLinkedListInParts.cpp
@@ -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
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