publicclassSolution { public ListNode[] SplitListToParts(ListNode head, int k) { int n = 0; ListNode temp = head; while (temp != null) { n++; temp = temp.next; } int quotient = n / k, remainder = n % k;
ListNode[] parts = new ListNode[k]; ListNode curr = head; for (int i = 0; i < k && curr != null; i++) { parts[i] = curr; int partSize = quotient + (i < remainder ? 1 : 0); for (int j = 1; j < partSize; j++) { curr = curr.next; } ListNode next = curr.next; curr.next = null; curr = next; } return parts; } }
classSolution: defsplitListToParts(self, head: ListNode, k: int) -> List[ListNode]: n = 0 node = head while node: n += 1 node = node.next quotient, remainder = n // k, n % k
parts = [Nonefor _ inrange(k)] i, curr = 0, head while i < k and curr: parts[i] = curr part_size = quotient + (1if i < remainder else0) for _ inrange(part_size - 1): curr = curr.next next = curr.next curr.next = None curr = next i += 1 return parts
var splitListToParts = function(head, k) { let n = 0; let temp = head; while (temp != null) { n++; temp = temp.next; } let quotient = Math.floor(n / k), remainder = n % k;
const parts = newArray(k).fill(null); let curr = head; for (let i = 0; i < k && curr != null; i++) { parts[i] = curr; let partSize = quotient + (i < remainder ? 1 : 0); for (let j = 1; j < partSize; j++) { curr = curr.next; } const next = curr.next; curr.next = null; curr = next; } return parts; };