JAVA | 100% Beats ✅ | Divide and Modulo
\n# Code\njava []\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode() {}\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        ListNode [] res = new ListNode[k];\n        int n = 0;\n        ListNode curr = head;\n        while (curr != null) {\n            n += 1;\n            curr = curr.next;\n        }\n        int size = n / k;\n        int rem = n % k;\n        curr = head;\n        ListNode prev = head;\n        for (int i = 0 ; i < k ; i++) {\n            ListNode part = curr;\n            int partSize = size;\n            if (rem > 0) {\n                rem -= 1;\n                partSize += 1;\n            }\n            for (int j = 0 ; j < partSize ; j++) {\n                prev = curr;\n                curr = curr.next;\n            }\n            if (prev != null) prev.next = null;\n            res[i] = part;\n        }\n        return res;\n    }\n}\n```
['Java']