Advertisement
kosievdmerwe

Untitled

Sep 29th, 2021
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. class Solution:
  2.     def splitListToParts(self, head: Optional[ListNode], k: int) -> List[Optional[ListNode]]:
  3.         N = 0
  4.        
  5.         cur = head
  6.         while cur:
  7.             N += 1
  8.             cur = cur.next
  9.            
  10.         ans = []
  11.         cur = head
  12.         for i in range(k):
  13.             ans.append(cur)
  14.             chunk_size = N // k + (1 if i < N % k else 0)
  15.             for j in range(chunk_size - 1):
  16.                 cur = cur.next
  17.            
  18.             if chunk_size > 0:
  19.                 old_cur = cur
  20.                 cur = cur.next
  21.                 old_cur.next = None
  22.         return ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement