Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def splitListToParts(self, head: Optional[ListNode], k: int) -> List[Optional[ListNode]]:
- N = 0
- cur = head
- while cur:
- N += 1
- cur = cur.next
- ans = []
- cur = head
- for i in range(k):
- ans.append(cur)
- chunk_size = N // k + (1 if i < N % k else 0)
- for j in range(chunk_size - 1):
- cur = cur.next
- if chunk_size > 0:
- old_cur = cur
- cur = cur.next
- old_cur.next = None
- return ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement