Re: [闲聊] 每日leetcode

楼主: dont   2024-09-08 11:52:17
725. Split Linked List in Parts
## 思路
先计算node的数量,
除k得到每堆的node数量, 剩下的余数平均给前r堆
## Code
```python
class Solution:
def splitListToParts(self, head: Optional[ListNode], k: int) ->
List[Optional[ListNode]]:
curr = head
count = 0
while curr:
count += 1
curr = curr.next
count, remains = divmod(count, k)
res = []
curr = head
for i in range(k):
res.append(curr)
for _ in range(count+(i < remains)-1):
curr = curr.next
if curr:
curr.next, curr = None, curr.next
return res
```
作者: sustainer123 (caster)   2024-09-08 11:53:00
大师周赛好难哦==
楼主: dont   2024-09-08 11:54:00
我睡过头了 要参加周赛好难
作者: sustainer123 (caster)   2024-09-08 11:55:00
我感觉我要掉分了 早知道睡过头

Links booklink

Contact Us: admin [ a t ] ucptt.com