Easy O(N) Solution | Beats 100%
Complexity\n- Time complexity: O(N)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(K)\n Add your space complexity here, e.g. O(n) \n\n\n\n
# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(K)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n![Screenshot 2024-09-08 062806.png](https://assets.leetcode.com/users/images/fec977d4-0cf5-417a-93fa-1b959d87fbb1_1725757234.1139126.png)\n\n# Code\n```python []\nclass Solution(object):\n    def splitListToParts(self, head, k):\n        # Step 1: Calculate the length of the linked list\n        n = 0\n        curr = head\n        while curr:\n            n += 1  # Increment the length correctly\n            curr = curr.next\n        \n        # Step 2: Determine the base size and how many parts need an extra node\n        base = n // k  # Minimum size of each part\n        extra = n % k  # Number of parts that get an extra node\n        \n        # Step 3: Split the linked list\n        res = []\n        curr = head\n        for i in range(k):\n            part_head = curr  # This is the head of the current part\n            part_size = base + (1 if extra > 0 else 0)  # Add 1 more node to the first \'extra\' parts\n            extra -= 1  # Decrease extra after assigning an extra node\n            \n            # Now, advance the pointer `part_size - 1` steps to get to the end of the current part\n            for _ in range(part_size - 1):\n                if curr:\n                    curr = curr.next\n            \n            # After advancing, break the current part if necessary\n            if curr:\n                next_part = curr.next  # The head of the next part\n                curr.next = None  # Break the list here\n                curr = next_part  # Move to the next part\n            \n            res.append(part_head)  # Add the head of the current part to the result\n        \n        return res\n```\n```C++ []\nclass Solution {\npublic:\n    vector<ListNode*> splitListToParts(ListNode* head, int k) {\n        // Step 1: Calculate the length of the linked list\n        int n = 0;\n        ListNode* curr = head;\n        while (curr) {\n            n++;  // Increment the length\n            curr = curr->next;\n        }\n        \n        // Step 2: Determine the base size and how many parts need an extra node\n        int base = n / k;  // Minimum size of each part\n        int extra = n % k;  // Number of parts that get an extra node\n        \n        // Step 3: Split the linked list\n        vector<ListNode*> res;\n        curr = head;\n        for (int i = 0; i < k; i++) {\n            ListNode* part_head = curr;  // This is the head of the current part\n            int part_size = base + (extra > 0 ? 1 : 0);  // Add 1 extra node if needed\n            extra--;  // Decrease extra after assigning the extra node\n            \n            // Now, advance the pointer `part_size - 1` steps to get to the end of the current part\n            for (int j = 0; j < part_size - 1 && curr; j++) {\n                curr = curr->next;\n            }\n            \n            // After advancing, break the current part if necessary\n            if (curr) {\n                ListNode* next_part = curr->next;  // The head of the next part\n                curr->next = nullptr;  // Break the list here\n                curr = next_part;  // Move to the next part\n            }\n            \n            res.push_back(part_head);  // Add the head of the current part to the result\n        }\n        \n        return res;\n    }\n};\n```\n```Java []\nclass Solution {\n    public ListNode[] splitListToParts(ListNode head, int k) {\n        // Step 1: Calculate the length of the linked list\n        int n = 0;\n        ListNode curr = head;\n        while (curr != null) {\n            n++;  // Increment the length\n            curr = curr.next;\n        }\n        \n        // Step 2: Determine the base size and how many parts need an extra node\n        int base = n / k;  // Minimum size of each part\n        int extra = n % k;  // Number of parts that get an extra node\n        \n        // Step 3: Split the linked list\n        ListNode[] res = new ListNode[k];\n        curr = head;\n        for (int i = 0; i < k; i++) {\n            ListNode part_head = curr;  // This is the head of the current part\n            int part_size = base + (extra > 0 ? 1 : 0);  // Add 1 extra node if needed\n            extra--;  // Decrease extra after assigning the extra node\n            \n            // Now, advance the pointer `part_size - 1` steps to get to the end of the current part\n            for (int j = 0; j < part_size - 1 && curr != null; j++) {\n                curr = curr.next;\n            }\n            \n            // After advancing, break the current part if necessary\n            if (curr != null) {\n                ListNode next_part = curr.next;  // The head of the next part\n                curr.next = null;  // Break the list here\n                curr = next_part;  // Move to the next part\n            }\n            \n            res[i] = part_head;  // Add the head of the current part to the result\n        }\n        \n        return res;\n    }\n}\n```\n![unnamed.jpg](https://assets.leetcode.com/users/images/b3c0dbc1-4245-41b5-899c-1c823ac29dc9_1725757296.9366615.jpeg)\n
['Linked List', 'Python', 'C++', 'Java', 'Python3']