Easy To Understand | java | 🔥🔥
Intuition\n Describe your first thoughts on how to solve this problem. \nwe can solve this problem using 2 methods one is creating a new Listnode and another is
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nwe can solve this problem using 2 methods one is creating a new Listnode and another is modifing the exesting Listnode. we are here going to implement the first one aproatch...\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTO solve this problem we have created a new ListNode() which will go on adding the Nodes in ListNode() accouring to the devided size\nthe main logic of the code is devide and mod operator which is very importent part which is needed to understand here.the rest of the part is very easy.\n\n# Complexity\n- Time complexity:O(n square)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\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 cur = head;\n        int N = 0;\n        while (cur != null) {\n            cur = cur.next;\n            N++;\n        }\n\n        int width = N / k, rem = N % k;\n\n        ListNode[] ans = new ListNode[k];\n        cur = head;\n        for (int i = 0; i < k; ++i) {\n            ListNode head1 = new ListNode(0), write = head1;\n            for (int j = 0; j < width + (i < rem ? 1 : 0); ++j) {\n                write = write.next = new ListNode(cur.val);\n                if (cur != null) cur = cur.next;\n            }\n            ans[i] = head1.next;\n        }\n        return ans;\n        \n        \n    }\n}\n\n```\n![upvotee.png](https://assets.leetcode.com/users/images/ef6f4b95-5d81-4012-af67-e60891f88ac8_1694001026.3266716.png)\n
['Java']