Python | nested loop | two-pointers | TC: O(n) SC: O(k)
Count the length of the linked list.\n2. Calculate the base size of each part and the number of parts that contain one more extra number. For example, if we hav
1. Count the length of the linked list.\n2. Calculate the base size of each part and the number of parts that contain one more extra number. For example, if we have 10 nodes, we have 2 parts with 3 number and 1 part with 3+1 = 4 number.\n*base size = 10//3 = 3* | *extra = 10%3 = 1* \n3. Use two pointers to record the previous and current node.\n4. For the nested loop, the outer loop record each split part, the inner one loops over the number and cut off by pointing prev to None.\n```\n# Definition for singly-linked list.\n# class ListNode:\n#     def __init__(self, val=0, next=None):\n#         self.val = val\n#         self.next = next\nclass Solution:\n    def splitListToParts(self, head: Optional[ListNode], k: int) -> List[Optional[ListNode]]:\n        length = 0\n        cur = head\n        while cur:\n            length += 1\n            cur = cur.next\n        # DON\'T do following since this makes head become null\n        # while head:\n        #     length += 1\n        #     head = head.next\n        \n        # calculate the base size and the number of parts contain extra number\n        size, extra = length // k, length % k\n        \n        # create empty list to store split parts\n        res = [[] for _ in range(k)]\n        \n        # use two ptrs to split parts\n        prev, cur = None, head\n        \n        for i in range(k):\n            res[i] = cur\n            # if this part contains extra number, it has (size+1) number\n            for j in range(size + (1 if extra > 0 else 0)):\n                prev, cur = cur, cur.next\n            if prev: prev.next = None\n            extra -= 1\n        \n        return res\n```
['Two Pointers', 'Python']