From b863302967a36a760e5e4599e1df070239d205ed Mon Sep 17 00:00:00 2001 From: Gabriel Donnan <47415809+gabedonnan@users.noreply.github.com> Date: Wed, 6 Sep 2023 16:47:58 +0100 Subject: [PATCH] Create split-linked-list.py Beats 97% --- split-linked-list.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 split-linked-list.py diff --git a/split-linked-list.py b/split-linked-list.py new file mode 100644 index 0000000..cbb339f --- /dev/null +++ b/split-linked-list.py @@ -0,0 +1,44 @@ +# 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]]: + lists = [None for _ in range(k)] + length = 0 + temp_head = head + while temp_head is not None: + length += 1 + temp_head = temp_head.next + remainder = (length % k) if length > k else 0 + node_size = max(length // k, 1) + temp_head = head + index = 0 + ctr = node_size + (1 if remainder != 0 else 0) + remainder = max(remainder - 1, 0) + lists[0] = temp_head + while temp_head is not None: + + ctr -= 1 + if ctr == 0: + ctr = node_size + (1 if remainder != 0 else 0) + remainder = max(remainder - 1, 0) + index += 1 + tmp = temp_head.next + temp_head.next = None + temp_head = tmp + try: + lists[index] = temp_head + except IndexError: + pass + + else: + temp_head = temp_head.next + + # print(lists) + return lists + + + +