Concise Explanation: Splitting Linked List into Equal Parts
Approach\n- Calculate the size of the linked list (N) using a helper function.\n\n- Determine the base size of each part as N / k and how many parts will have a
# Approach\n- Calculate the size of the linked list (N) using a helper function.\n\n- Determine the base size of each part as N / k and how many parts will have an extra node (plusOne = N % k).\n\n- Initialize an array ans of size k to store the heads of each part, initially set to nullptr.\n\n- Iterate through the parts and traverse the linked list for partSize nodes. Increment partSize by 1 for the first plusOne parts.\n\n- Set the next pointer of the current node to nullptr to split the list.\n\n- Move to the next part and update its head to the node following the last processed node.\n\n- Repeat steps 4-6 until all parts are processed.\n\n- Return the ans array containing the heads of the split linked list parts.\n\n# Code\n```\nclass Solution {\n  int sizeOfLL(ListNode* head);\npublic:\n  vector<ListNode*> splitListToParts(ListNode* head, int k) {\n\n    vector<ListNode*>ans(k, nullptr);\n\n    int N = sizeOfLL(head), partSize = N / k, plusOne = N % k, iterCount = 0;\n\n    ans[iterCount] = head;\n\n    while(true){\n\n      if(ans[iterCount] == nullptr)break;\n\n      int count = partSize;\n\n      if(iterCount >= plusOne && --count);\n\n      ListNode* curr = ans[iterCount];\n\n      while(curr && count--)curr = curr->next;\n      \n      ListNode* nextNode = curr->next;\n\n      if(curr->next)curr->next = nullptr;\n\n      if(++iterCount == k)break;\n\n      ans[iterCount] = nextNode;\n    }\n    return ans;\n  }\n};\n\n\nint Solution::sizeOfLL(ListNode* head){\n  int count = 0;\n  while(head && ++count)head = head->next;\n  return count;\n}\n```
['Linked List', 'C++']