【Video】Simple and Easy Solution  - Python, JavaScript, Java, C++
Intuition\nCalculate length of linked list and get length of each sub list.\n\n---\n\n# Solution Video\n\n### Please subscribe to my channel from here. I have 2
# Intuition\nCalculate length of linked list and get length of each sub list.\n\n---\n\n# Solution Video\n\n### Please subscribe to my channel from here. I have 256 videos as of September 6th, 2023.\n\nhttps://youtu.be/ir-tDj1kM5g\n\n### In the video, the steps of approach below are visualized using diagrams and drawings. I\'m sure you understand the solution easily!\n\n### \u2B50\uFE0F\u2B50\uFE0F Don\'t forget to subscribe to my channel! \u2B50\uFE0F\u2B50\uFE0F\n\n**\u25A0 Subscribe URL**\nhttp://www.youtube.com/channel/UC9RMNwYTL3SXCP6ShLWVFww?sub_confirmation=1\n\n---\n\n# Approach\nThis is based on Python. Other might be different a bit.\n\n1. **Calculate the length of the linked list**\n   - Initialize a variable `length` to 0 to keep track of the number of nodes in the linked list.\n   - Initialize a pointer `current` to the head of the linked list.\n   - Use a `while` loop to traverse the linked list while updating `current` and incrementing `length` for each node in the list.\n\n2. **Determine the size of each part**\n   - Calculate `part_size` by dividing `length` by `k`. This gives you the base size of each part.\n   - Calculate `larger_parts` by taking the remainder of `length` divided by `k`. This represents the number of parts that will have one extra element.\n\n3. **Split the linked list into parts**\n   - Initialize an empty list `result` to store the resulting linked list parts.\n   - Reset the `current` pointer to the head of the linked list.\n   - Use a `for` loop to iterate `k` times, where `k` is the number of parts to split the list into.\n   - Calculate `sublist_size` based on whether the current part (`i`) should have an extra element. If `i` is less than `larger_parts`, add 1 to `part_size`; otherwise, use `part_size`.\n   - If `sublist_size` is 0 (which can happen if `k` is greater than the length of the linked list), append `None` to the `result` list to represent an empty part.\n   - Otherwise, create a `sublist_head` pointer to the current node, which will be the head of the sublist.\n   - Use a nested `for` loop to traverse the sublist until you reach the end of the sublist (which is `sublist_size - 1` nodes). Update the `current` pointer accordingly.\n   - Store the next node (`next_node`) after the sublist in `next_node`.\n   - Set the `next` pointer of the current node to `None` to separate the sublist from the original list.\n   - Append the `sublist_head` to the `result` list, representing one of the parts of the split linked list.\n   - Update the `current` pointer to `next_node` to prepare for the next iteration.\n\n4. **Return the result**\n   - Return the `result` list, which contains the linked list split into parts.\n\nThis algorithm takes a linked list, calculates its length, determines the size of each part based on the desired number of parts, and then splits the linked list into those parts while maintaining the original order of elements.\n\n# Complexity\n- Time complexity: O(n) or O(n + k)\nDepends on size of n and k.\n\n- Space complexity: O(k)\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\nclass Solution:\n    def splitListToParts(self, head: Optional[ListNode], k: int) -> List[Optional[ListNode]]:\n        # Step 1: Calculate the length of the linked list\n        length = 0\n        current = head\n        while current:\n            length += 1\n            current = current.next\n        \n        # Step 2: Determine the size of each part\n        part_size = length // k\n        larger_parts = length % k\n        \n        # Initialize a list to store the results\n        result = []\n        \n        # Step 3: Split the linked list into parts\n        current = head\n        for i in range(k):\n            sublist_size = part_size + 1 if i < larger_parts else part_size\n            if sublist_size == 0:\n                result.append(None)\n            else:\n                sublist_head = current\n                for _ in range(sublist_size - 1):\n                    current = current.next\n                next_node = current.next\n                current.next = None\n                result.append(sublist_head)\n                current = next_node\n        \n        return result\n```\n```javascript []\n/**\n * Definition for singly-linked list.\n * function ListNode(val, next) {\n *     this.val = (val===undefined ? 0 : val)\n *     this.next = (next===undefined ? null : next)\n * }\n */\n/**\n * @param {ListNode} head\n * @param {number} k\n * @return {ListNode[]}\n */\nvar splitListToParts = function(head, k) {\n    // Step 1: Calculate the length of the linked list\n    let length = 0;\n    let current = head;\n    while (current) {\n        length += 1;\n        current = current.next;\n    }\n\n    // Step 2: Determine the size of each part\n    const partSize = Math.floor(length / k);\n    const largerParts = length % k;\n\n    // Initialize an array to store the results\n    const result = [];\n\n    // Step 3: Split the linked list into parts\n    current = head;\n    for (let i = 0; i < k; i++) {\n        const sublistSize = i < largerParts ? partSize + 1 : partSize;\n        if (sublistSize === 0) {\n            result.push(null);\n        } else {\n            const sublistHead = current;\n            for (let j = 0; j < sublistSize - 1; j++) {\n                current = current.next;\n            }\n            const nextNode = current.next;\n            current.next = null;\n            result.push(sublistHead);\n            current = nextNode;\n        }\n    }\n\n    return result;    \n};\n```\n```java []\n/**\n * Definition for singly-linked list.\n * public 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 */\nclass Solution {\n    public ListNode[] splitListToParts(ListNode head, int k) {\n        // Step 1: Calculate the length of the linked list\n        int length = 0;\n        ListNode current = head;\n        while (current != null) {\n            length += 1;\n            current = current.next;\n        }\n\n        // Step 2: Determine the size of each part\n        int partSize = length / k;\n        int largerParts = length % k;\n\n        // Initialize an array to store the results\n        ListNode[] result = new ListNode[k];\n\n        // Step 3: Split the linked list into parts\n        current = head;\n        for (int i = 0; i < k; i++) {\n            int sublistSize = (i < largerParts) ? (partSize + 1) : partSize;\n            if (sublistSize == 0) {\n                result[i] = null;\n            } else {\n                ListNode sublistHead = current;\n                for (int j = 0; j < sublistSize - 1; j++) {\n                    current = current.next;\n                }\n                ListNode nextNode = current.next;\n                current.next = null;\n                result[i] = sublistHead;\n                current = nextNode;\n            }\n        }\n\n        return result;        \n    }\n}\n```\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        // Step 1: Calculate the length of the linked list\n        int length = 0;\n        ListNode* current = head;\n        while (current) {\n            length += 1;\n            current = current->next;\n        }\n\n        // Step 2: Determine the size of each part\n        int partSize = length / k;\n        int largerParts = length % k;\n\n        // Initialize a vector to store the results\n        vector<ListNode*> result(k);\n\n        // Step 3: Split the linked list into parts\n        current = head;\n        for (int i = 0; i < k; i++) {\n            int sublistSize = (i < largerParts) ? (partSize + 1) : partSize;\n            if (sublistSize == 0) {\n                result[i] = nullptr;\n            } else {\n                ListNode* sublistHead = current;\n                for (int j = 0; j < sublistSize - 1; j++) {\n                    current = current->next;\n                }\n                ListNode* nextNode = current->next;\n                current->next = nullptr;\n                result[i] = sublistHead;\n                current = nextNode;\n            }\n        }\n\n        return result;        \n    }\n};\n```\n\n\n
['C++', 'Java', 'Python3', 'JavaScript']