Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions 725. Split Linked List in Parts1
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
};
Loading