Beats 100% || Step by step || Easy Explanation || C++ || Java || Python
\n# Problem Description:\nGiven the head of a singly-linked list and an integer k, you need to split the list into k consecutive linked list "parts." The size o
\n# Problem Description:\nGiven the head of a singly-linked list and an integer k, you need to split the list into k consecutive linked list "parts." The size of each part should be as equal as possible and parts occurring earlier should always have a size greater than or equal to parts occurring later.. If there are fewer than k nodes in the linked list, then empty nodes should be placed in the remaining parts.\n\n`for detailed explanation you can refer to my youtube channel` \n\n[ Video in Hindi click here](https://youtube.com/@LetsCodeTogether72/videos)\n\nor link in my profile.Here,you can find any solution in playlists monthwise from june 2023 with detailed explanation.i upload daily leetcode solution video with short and precise explanation.\n\n# Solution Approach:\nTo solve this problem, we will first find the total length of the linked list. Then, we will calculate the number of elements in each part and the number of extra elements. After that, we will iterate through the list and create sublists, taking care to distribute the extra elements as evenly as possible among the parts.\n\n# Code Explanation:\nLet\'s break down the code step by step:\n\n1. Calculate the length of the linked list by traversing it using a temporary pointer \'temp.\'\n\n2. Create a vector \'v\' of ListNode pointers with size \'k\' to store the resulting sublists.\n\n3. Calculate \'p,\' which represents the minimum number of elements in each part, by dividing the total length by \'k.\'\n\n4. Calculate \'extra,\' which represents the number of extra elements that need to be distributed among the parts, by taking the modulus of the total length by \'k.\'\n\n5. Initialize \'temp\' to the head of the linked list and \'j\' to 0, which will help us keep track of the current part we are working on.\n\n6. Iterate through the linked list, creating sublists:\n\n   a. Create a dummy node and a \'curr\' pointer to help build the sublist.\n   \n   b. For \'p\' times (or until we run out of elements), copy the value from the original list to the sublist.\n\n   c. If \'extra\' is greater than 0, copy an extra element to the sublist and decrement \'extra.\'\n\n   d. Assign the sublist (excluding the dummy node) to \'v[j]\' to store the result of this part.\n\n   e. Increment \'j\' to move on to the next part.\n\n7. Return the vector \'v,\' which contains the k sublists.\n\n# Complexity\n- Time complexity:$$O(n)$$ \n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(1)$$ \n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n``` C++ []\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode() : val(0), next(nullptr) {}\n *     ListNode(int x) : val(x), next(nullptr) {}\n *     ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n    vector<ListNode*> splitListToParts(ListNode* head, int k) {\n        int len=0;\n        ListNode*temp=head;\n        while(temp!=NULL){\n            len++;\n            temp=temp->next;\n        }\n        vector<ListNode*>v(k,NULL);\n        int p=len/k;\n        int extra=len%k;\n        temp=head;\n        int j=0;\n        while(temp!=NULL){\n            ListNode*c=temp;\n            ListNode*dummy=new ListNode(-1);\n            ListNode*curr=dummy;\n            for(int i=0;i<p;i++){\n                curr->next=new ListNode(temp->val);\n                temp=temp->next;\n                curr=curr->next;\n            }\n            if(extra>0){\n                curr->next=new ListNode(temp->val);\n                temp=temp->next;\n                curr=curr->next;\n                extra--;\n            }\n            v[j]=dummy->next;\n            j++;\n        }\n        return v;\n    }\n};\n```\n```java []\n/**\n * Definition for singly-linked list.\n * class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode() {}\n *     ListNode(int val) { this.val = val; }\n *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n */\n\nclass Solution {\n    public ListNode[] splitListToParts(ListNode head, int k) {\n        int length = 0;\n        ListNode temp = head;\n        while (temp != null) {\n            length++;\n            temp = temp.next;\n        }\n\n        int partSize = length / k;\n        int extra = length % k;\n\n        ListNode[] result = new ListNode[k];\n        temp = head;\n        for (int i = 0; i < k; i++) {\n            result[i] = temp;\n            int currentPartSize = partSize + (extra-- > 0 ? 1 : 0);\n            for (int j = 0; j < currentPartSize - 1; j++) {\n                temp = temp.next;\n            }\n            if (temp != null) {\n                ListNode next = temp.next;\n                temp.next = null;\n                temp = next;\n            }\n        }\n        return result;\n    }\n}\n\n```\n```python []\n# Definition for singly-linked list.\n# class ListNode:\n#     def __init__(self, val=0, next=None):\n#         self.val = val\n#         self.next = next\n\nclass Solution:\n    def splitListToParts(self, head: ListNode, k: int) -> List[ListNode]:\n        length = 0\n        temp = head\n        while temp:\n            length += 1\n            temp = temp.next\n\n        part_size = length // k\n        extra = length % k\n\n        result = []\n        temp = head\n        for i in range(k):\n            result.append(temp)\n            current_part_size = part_size + 1 if extra > 0 else part_size\n            extra -= 1\n            for j in range(current_part_size - 1):\n                temp = temp.next\n            if temp:\n                temp.next, temp = None, temp.next\n\n        return result\n\n```\n\n
['Linked List', 'Python', 'C++', 'Java', 'Python3']