-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathSplit Linked List in Parts.cpp
63 lines (50 loc) · 1.64 KB
/
Split Linked List in Parts.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Solution by Rahul Surana
***********************************************************
Given the head of a singly linked list and an integer k, 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 one.
This may lead to some parts being null.
The parts should be in the order of occurrence in the input list, and parts occurring earlier should
always have a size greater than or equal to parts occurring later.
Return an array of the k parts.
***********************************************************
*/
/**
* 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* head, int k) {
int n = 0;
ListNode* x = head;
while(x!= NULL) {
x = x->next;
n++;
}
int per = n/k;
int rem = n %k;
vector<ListNode*> ans(k,NULL);
x = head;
int c = 0, i = 0;
while(x!= NULL){
ans[i] = (x);
while(x && c < (per+(rem>0))-1) { x = x->next; c++; }
if(x){
ListNode* z = x->next;
x->next = NULL;
x = z;
}
rem--;
c = 0;
i++;
}
return ans;
}
};