Simple Java || C++ Code ☠️
Code\njava []\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode() {}\n *     List
# Code\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        ListNode itr = head;\n        int n = 0;\n        while(itr != null){\n            n++;\n            itr = itr.next;\n        }\n        int x = (n/k);\n        int y = (n%k);\n        ListNode[] arr = new ListNode[k];\n        int a = 0,i=0;\n        if(y != 0){\n            a = 1;\n        }\n        itr = head;\n        for(i=0;i<k;i++){\n            ListNode it = itr;\n            arr[i]=it;\n            int b = (x+a)-1;\n            while(b>0){\n                it = it.next;\n                b--;\n            }\n            y--;\n            if(y==0){\n                a=0;\n            }\n            if(it == null){\n                break;\n            }\n            itr = it.next;\n            it.next = null;\n        }\n        while(i<k){\n            arr[i]=null;\n            i++;\n        }\n        return arr;\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        ListNode* itr = head;\n        int n = 0;\n        while(itr != NULL){\n            n++;\n            itr = itr->next;\n        }\n        int x = (n/k);\n        int y = (n%k);\n        vector<ListNode*> arr(k);\n        int a = 0,i=0;\n        if(y != 0){\n            a = 1;\n        }\n        itr = head;\n        for(i=0;i<k;i++){\n            ListNode* it = itr;\n            arr[i]=it;\n            int b = (x+a)-1;\n            while(b>0){\n                it = it->next;\n                b--;\n            }\n            y--;\n            if(y==0){\n                a=0;\n            }\n            if(it == NULL){\n                break;\n            }\n            itr = it->next;\n            it->next = NULL;\n        }\n        while(i<k){\n            arr[i]=NULL;\n            i++;\n        }\n        return arr;\n    }\n};\n```\n\n\n\n### Explanation\n\n#### Step 1: Count the Total Number of Nodes\n```java\nListNode itr = head;\nint n = 0;\nwhile(itr != null){\n    n++;\n    itr = itr.next;\n}\n```\n- The first step is to count the total number of nodes (`n`) in the linked list.\n- We initialize a pointer `itr` to traverse the linked list and count the number of nodes.\n\n#### Step 2: Calculate the Base Size and Extra Nodes\n```java\nint x = (n/k);   // Base size of each part\nint y = (n%k);   // Number of parts that will have one extra node\n```\n- **`x = n / k`**: This is the base size of each part. Every part will have at least `x` nodes.\n- **`y = n % k`**: This represents the number of parts that will have one extra node. If `n` is not perfectly divisible by `k`, the first `y` parts will have one more node than the rest.\n\n#### Step 3: Initialize the Array to Store Parts\n```java\nListNode[] arr = new ListNode[k];\n```\n- We create an array `arr[]` of size `k` to store the head of each part of the linked list.\n\n#### Step 4: Traverse the List and Split Into Parts\n```java\nint a = 0,i=0;\nif(y != 0){\n    a = 1;\n}\nitr = head;\nfor(i=0;i<k;i++){\n    ListNode it = itr;\n    arr[i]=it;\n    int b = (x+a)-1;\n    while(b>0){\n        it = it.next;\n        b--;\n    }\n    y--;\n    if(y==0){\n        a=0;\n    }\n    if(it == null){\n        break;\n    }\n    itr = it.next;\n    it.next = null;\n}\n```\n\n- We then traverse the linked list and split it into parts:\n  1. **Variables**:\n     - `a`: This flag is used to decide if the current part should have one extra node (`a = 1` if `y > 0`, meaning extra nodes are still to be distributed).\n     - `b`: This is the size of the current part, which is calculated as `(x + a) - 1` (because the `while` loop is used to move `b` nodes forward).\n  2. **Outer Loop**: This runs `k` times, one for each part:\n     - For each part `i`, we set `arr[i]` to point to the current node (`itr`), which will become the head of this part.\n     - Then, using a nested `while` loop, we traverse `b` nodes, where `b` is the size of the current part.\n  3. **Handle Remaining Extra Nodes**: After each part is processed:\n     - We decrease `y`, and once `y` reaches zero, `a` is set to `0`, meaning no more parts will have an extra node.\n     - After reaching the end of a part, we cut the link by setting `it.next = null` to split it from the rest of the list.\n     - Then, move `itr` to the next part\u2019s starting node.\n\n#### Step 5: Handle Parts if `k > n`\n```java\nwhile(i<k){\n    arr[i]=null;\n    i++;\n}\n```\n- If `k` is greater than `n` (i.e., the number of parts requested is more than the number of nodes), the remaining parts are set to `null`. This is done by looping through the array and setting the remaining parts to `null`.\n\n#### Step 6: Return the Result\n```java\nreturn arr;\n```\n- Finally, the array of parts is returned.\n\n### Example Walkthrough\n\nLet\u2019s walk through an example:\n\n**Linked List**: 1 \u2192 2 \u2192 3 \u2192 4 \u2192 5 \u2192 6 \u2192 7 \u2192 8 \u2192 9  \n**k = 3**\n\n1. **Step 1**: Count total nodes (`n` = 9).\n\n2. **Step 2**: Calculate `x` and `y`:\n   - `x = n / k = 9 / 3 = 3` (Each part will have at least 3 nodes).\n   - `y = n % k = 9 % 3 = 0` (There are no extra nodes to distribute).\n\n3. **Step 3-4**: Split into 3 parts:\n   - Part 1: 1 \u2192 2 \u2192 3\n   - Part 2: 4 \u2192 5 \u2192 6\n   - Part 3: 7 \u2192 8 \u2192 9\n\n4. **Step 5**: No need to fill parts with `null` since `k` equals `n`.\n\n**Output**: An array of 3 linked lists:\n- `arr[0]` \u2192 1 \u2192 2 \u2192 3\n- `arr[1]` \u2192 4 \u2192 5 \u2192 6\n- `arr[2]` \u2192 7 \u2192 8 \u2192 9\n\n### Time Complexity\n- **O(n + k)**: We first traverse the entire list to count the nodes (`O(n)`), and then we split the list into `k` parts (`O(n)`), making the overall complexity `O(n + k)`.\n\n### Space Complexity\n- **O(k)**: We are using an array of size `k` to store the `k` parts, so the space complexity is `O(k)`. The linked list nodes themselves are not extra memory as they already exist.\n\n\n
['Linked List', 'C++', 'Java']