Dcc 8/09/2024 || LinkedList || Beats 100% submissions
Approach and Intuition:\nThe problem is to split a singly-linked list into k parts as evenly as possible. Each part should either contain the same number of nod
### Approach and Intuition:\nThe problem is to split a singly-linked list into `k` parts as evenly as possible. Each part should either contain the same number of nodes or differ by at most one node.\n\n1. **Count Total Nodes**: First, traverse the linked list to count the total number of nodes.\n2. **Determine Part Sizes**: \n   - Compute the base size of each part as `total_nodes / k`.\n   - If `total_nodes % k` is non-zero, distribute the remaining nodes (`remaining_size = total_nodes % k`) among the first `remaining_size` parts, so that those parts will have one extra node.\n3. **Split the List**: Traverse the list again, creating a new list for each part according to the sizes computed. Use a loop to split the list into the appropriate number of nodes, adjusting the `next` pointer to break the list after the required number of nodes for each part.\n\n### Complexity Analysis:\n1. **Time Complexity**:\n   - **O(n)** for counting the number of nodes (`n` is the number of nodes in the list).\n   - **O(k)** for distributing the remainder of nodes among the first few parts.\n   - **O(n)** for traversing the list again to split it into parts.\n   Overall time complexity: **O(n)**.\n\n2. **Space Complexity**:\n   - The space used is for the `size_of_elements` array of size `k`, and an answer array of size `k` to store the parts.\n   Thus, the space complexity is **O(k)**.\n\nThis approach ensures that we split the linked list into `k` parts as evenly as possible. If there are fewer nodes than `k`, the extra parts will contain `null`.\n\n# 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        int[] size_of_elements = new int[k];\n        ListNode curr = head;\n        int count_nodes=0;\n        while(curr!=null){\n\n            count_nodes++;\n            curr = curr.next;\n        }\n        int size = count_nodes/k;\n        int remaining_size = count_nodes%k;\n        Arrays.fill(size_of_elements,size);\n        int ind=0;\n        while(remaining_size!=0){\n            size_of_elements[ind++]++;\n            remaining_size--;\n        }\n        curr = head;\n        // System.out.println(Arrays.toString(size_of_elements));\n        ListNode[] answer = new ListNode[k];\n        for(int i=0;i<k;i++){\n\n            int curr_size =1;\n            while(curr_size!=size_of_elements[i] && curr!=null){\n                curr_size++;\n                curr = curr.next;\n            }\n            ListNode new_head = null;\n            if(curr!=null){\n                 new_head = curr.next;\n                 curr.next = null;\n            }\n            answer[i] = head;\n            head = new_head;\n            curr = new_head;\n            if(curr==null) break; \n        }\n        return answer;\n\n        \n    }\n}\n```
['Linked List', 'Java']