Skip to content

Latest commit

 

History

History
82 lines (60 loc) · 2 KB

_725. Split Linked List in Parts.md

File metadata and controls

82 lines (60 loc) · 2 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 22, 2024

Last updated : June 22, 2024


Related Topics : Linked List

Acceptance Rate : 69.95 %


Solutions

Python

# 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: Optional[ListNode], k: int) -> List[Optional[ListNode]]:
        listLength = 0

        curr = head
        while curr :
            curr = curr.next
            listLength += 1

        chunkSize = listLength // k
        numberOfLargerChunks = listLength - chunkSize * k
        numberOfRegularChunks = k - numberOfLargerChunks
        output = []

        prev = None
        curr = head

        while curr :
            if numberOfLargerChunks :
                numberOfLargerChunks -= 1
                currentChunkSize = chunkSize + 1

                output.append(curr)
                if prev :
                    prev.next = None
                
                for i in range(currentChunkSize) :
                    prev = curr
                    curr = curr.next
            
            elif numberOfRegularChunks :
                numberOfRegularChunks -= 1
                
                output.append(curr)
                if prev :
                    prev.next = None
                
                for i in range(chunkSize) :
                    prev = curr 
                    curr = curr.next

            else :
                break

        while numberOfRegularChunks :
            output.append(None)
            numberOfRegularChunks -= 1

        return output