-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path725. Split Linked List in Parts
45 lines (40 loc) · 1.5 KB
/
725. Split Linked List in Parts
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
/**
* 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) {
// Create a vector of ListNode pointers to store the k parts.
vector<ListNode*> parts(k, nullptr);
// Calculate the length of the linked list.
int len = 0;
for (ListNode* node = root; node; node = node->next)
len++;
// Calculate the minimum guaranteed part size (n) and the number of extra nodes (r).
int n = len / k, r = len % k;
// Initialize pointers to traverse the linked list.
ListNode* node = root, *prev = nullptr;
// Loop through each part.
for (int i = 0; node && i < k; i++, r--) {
// Store the current node as the start of the current part.
parts[i] = node;
// Traverse n + 1 nodes if there are remaining extra nodes (r > 0).
// Otherwise, traverse only n nodes.
for (int j = 0; j < n + (r > 0); j++) {
prev = node;
node = node->next;
}
// Disconnect the current part from the rest of the list by setting prev->next to nullptr.
prev->next = nullptr;
}
// Return the array of k parts.
return parts;
}
};