Skip to content

Latest commit

 

History

History
16 lines (14 loc) · 1.15 KB

File metadata and controls

16 lines (14 loc) · 1.15 KB

Calculate the length of the linked list. Initialize two counters, base_size and extra, to the length of the linked list divided by k and the remainder of the division, respectively. Initialize a pointer, current, to the head of the linked list. Iterate over the range [0, k - 1]: Initialize two pointers, part_head and part_tail, to null. Iterate over the range [0, base_size + (extra > 0 ? 1 : 0) - 1]: If part_head is null, then set part_head and part_tail to the current node. Otherwise, set part_tail.next to the current node and update part_tail to the current node. Update current to the next node in the linked list. Set part_tail.next to null. Add part_head to the list of parts. Decrement extra by 1. Return the list of parts.

This algorithm works by greedily splitting the linked list into k parts of approximately equal size. The base_size variable represents the number of nodes in each part, and the extra variable represents the number of extra nodes that are added to the first extra parts. The algorithm iterates over the linked list, adding each node to the current part until the part is full. Then, the algorithm starts a new part and repeats the process.