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
上一篇:力扣 273. 整数转换英文表示


下一篇:链表反转