其他分享
首页 > 其他分享> > Leetcode#725. 分割链表

Leetcode#725. 分割链表

作者:互联网

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def splitListToParts(self, head: ListNode, k: int) -> List[ListNode]:
        length = 0
        p = head 
        while p != None:
            length += 1
            p = p.next

        basic = length // k
        remain = length % k

        curr = head 
        index = 0
        ans = [None] * k

        while curr:
            ans[index] = curr
            last = None
            curr_len = basic + 1 if index < remain else basic
            for i in range(curr_len):
                last = curr
                curr = curr.next 
            last.next = None
            index += 1
        return ans

标签:index,None,curr,self,next,链表,length,725,Leetcode
来源: https://blog.csdn.net/sinat_20177327/article/details/120606790