- 分隔链表
class Solution:
def splitListToParts(self, root: ListNode, k: int) -> List[ListNode]:
cur=root
length=0
while cur:
length+=1
cur=cur.next
changdu=length//k
shang=length%k
cur=root
ans=[]
for i in range(k):
ans.append(cur)
size=changdu+(1 if shang>0 else 0)
if cur:
for j in range(size-1):
cur=cur.next
tmp=cur.next
#链表这里只要截断一次,append里面的cur就会截断
cur.next=None
cur=tmp
shang-=1
return ans