|
1 | 1 | # [Problem 725: Split Linked List in Parts](https://leetcode.com/problems/split-linked-list-in-parts/description/?envType=daily-question)
|
2 | 2 |
|
3 | 3 | ## Initial thoughts (stream-of-consciousness)
|
| 4 | +- There are two parts to this problem: figuring out the sizes of each list segment and actually doing the splitting |
| 5 | +- Let's consider how to solve each in turn |
4 | 6 |
|
5 | 7 | ## Refining the problem, round 2 thoughts
|
| 8 | +- To figure out the sizes of the segments, I think we can: |
| 9 | + - First, figure out how long the list is with an initial pass through the list. Let's say the length is `n`. |
| 10 | + - We can then compute a "base" size of each segment as `n // k`. This is the "default" segment length, and it's also the minimum segment lenght. |
| 11 | + - But some of the segments might need to be augmented by 1 additional element. The number of segments that need augmenting is `n % k`. We can create an `extra = n % k` variable, and then while `extra > 0` we augment the given segment with an additional element and then decrement `extra`. |
| 12 | +- Splitting the segments requires setting the `next` property of the last element of the previous segment to `None` and then appending the first element of the next segment to the to-be-outputted list of segments. |
| 13 | +- Note: I'm assuming that the output should be a list of heads of each segment's linked list, as opposed to "regular" (`list`) lists of the values of each segment's nodes |
| 14 | +- Assuming that this is the desired output format, I think I'm ready to implement the solution |
6 | 15 |
|
7 | 16 | ## Attempted solution(s)
|
8 | 17 | ```python
|
9 |
| -class Solution: # paste your code here! |
10 |
| - ... |
| 18 | +# Definition for singly-linked list. |
| 19 | +# class ListNode: |
| 20 | +# def __init__(self, val=0, next=None): |
| 21 | +# self.val = val |
| 22 | +# self.next = next |
| 23 | +class Solution: |
| 24 | + def splitListToParts(self, head: Optional[ListNode], k: int) -> List[Optional[ListNode]]: |
| 25 | + # first compute the length of the list |
| 26 | + n = 0 |
| 27 | + node = head |
| 28 | + while node is not None: |
| 29 | + node = node.next |
| 30 | + n += 1 |
| 31 | + |
| 32 | + # now compile the actual segments |
| 33 | + base_length = n // k |
| 34 | + extras = n % k |
| 35 | + |
| 36 | + segments = [] |
| 37 | + node = head |
| 38 | + |
| 39 | + for i in range(k): |
| 40 | + part_head = node |
| 41 | + part_length = base_length + (1 if extras > 0 else 0) |
| 42 | + extras -= 1 if extras > 0 else 0 |
| 43 | + |
| 44 | + # finish off this segment |
| 45 | + for j in range(part_length - 1): |
| 46 | + if node: |
| 47 | + node = node.next |
| 48 | + |
| 49 | + # tie off the current segment and get ready for the next one |
| 50 | + if node: |
| 51 | + next_part = node.next |
| 52 | + node.next = None |
| 53 | + node = next_part |
| 54 | + |
| 55 | + segments.append(part_head) |
| 56 | + |
| 57 | + return segments |
11 | 58 | ```
|
| 59 | +- Given examples pass |
| 60 | +- Submitting... |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +Solved! |
| 65 | + |
0 commit comments