# LeetCode: Split Linked List in Parts Solution

Naming is hard

## Approach

Let

n
be the size of the list

Each part of

k
parts will have
n / k
elements plus extra 1 element if the part index is less than
n % k

Naming explanation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}dividend = divisor * quotient + remainder2
3eg. 10 = 3 * 3 + 1```

## Implementation

```1var splitListToParts = function (head, k) {2  let n = 03  let iter = head4  let res = Array(k).fill(null)5
6  for (; iter; iter = iter.next, n++) {}7
8  // naming explanation:9  // dividend = divisor * quotient + remainder10  let quotient = Math.floor(n / k)11  let remainder = n % k12
15  for (let part = 0; part < k && iter; part++) {16    let partSize = quotient + (part < remainder ? 1 : 0)17
18    // init first element19    let prev = (res[part] = new ListNode(iter.val))20    iter = iter.next21    partSize--22
23    // add the rest24    while (partSize-- && iter) {25      prev.next = new ListNode(iter.val)26      prev = prev.next27      iter = iter.next28    }29  }30
31  return res32}```

Original problem

Rotate List

leetcode

## Next Post

LeetCode: Sort Colors

Sep 26, 2022

Count and overwrite

Search Posts