# 725

from utils.listNode import ListNode

class Solution:
    def split(self, head, k):
        if not head:
            return []
        
        nodes_count = self.get_count(head)
        part_length, odd_parts = divmod(nodes_count, k)

        result = []
        prev, node = None, head

        for _ in range(k):
            required = part_length
            if odd_parts:
                required += 1
                odd_parts -= 1
            
            result.append(node)

            for _ in range(required):
                # we'll only get here if required > 0, i.e. node is not null
                prev, node = node, node.next

            if prev:
                prev.next = None
        
        return result
    
    def get_count(self, head):
        count = 0

        while head:
            head = head.next
            count += 1
        
        return count

one = ListNode(1)
two = ListNode(2)
three = ListNode(3)
four = ListNode(4)
five = ListNode(5)
six = ListNode(6)
seven = ListNode(7)

one.next = two
two.next = three
three.next = four
four.next = five
five.next = six
six.next = seven

print(one)

solution = Solution()
print(solution.split(one, 10))