Skip to content

Commit 55cff60

Browse files
committed
Sync LeetCode submission Runtime - 42 ms (27.68%), Memory - 16.8 MB (91.02%)
1 parent eb3e01d commit 55cff60

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<p>Given the <code>head</code> of a singly linked list and an integer <code>k</code>, split the linked list into <code>k</code> consecutive linked list parts.</p>
2+
3+
<p>The length of each part should be as equal as possible: no two parts should have a size differing by more than one. This may lead to some parts being null.</p>
4+
5+
<p>The parts should be in the order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal to parts occurring later.</p>
6+
7+
<p>Return <em>an array of the </em><code>k</code><em> parts</em>.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/13/split1-lc.jpg" style="width: 400px; height: 134px;" />
12+
<pre>
13+
<strong>Input:</strong> head = [1,2,3], k = 5
14+
<strong>Output:</strong> [[1],[2],[3],[],[]]
15+
<strong>Explanation:</strong>
16+
The first element output[0] has output[0].val = 1, output[0].next = null.
17+
The last element output[4] is null, but its string representation as a ListNode is [].
18+
</pre>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/13/split2-lc.jpg" style="width: 600px; height: 60px;" />
22+
<pre>
23+
<strong>Input:</strong> head = [1,2,3,4,5,6,7,8,9,10], k = 3
24+
<strong>Output:</strong> [[1,2,3,4],[5,6,7],[8,9,10]]
25+
<strong>Explanation:</strong>
26+
The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts.
27+
</pre>
28+
29+
<p>&nbsp;</p>
30+
<p><strong>Constraints:</strong></p>
31+
32+
<ul>
33+
<li>The number of nodes in the list is in the range <code>[0, 1000]</code>.</li>
34+
<li><code>0 &lt;= Node.val &lt;= 1000</code></li>
35+
<li><code>1 &lt;= k &lt;= 50</code></li>
36+
</ul>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Approach 2: Split Input List
2+
3+
# Time: O(N + k)
4+
# Space: O(k)
5+
6+
# Definition for singly-linked list.
7+
# class ListNode:
8+
# def __init__(self, val=0, next=None):
9+
# self.val = val
10+
# self.next = next
11+
class Solution:
12+
def splitListToParts(self, head: Optional[ListNode], k: int) -> List[Optional[ListNode]]:
13+
curr = head
14+
for N in range(1001):
15+
if not curr:
16+
break
17+
curr = curr.next
18+
19+
width, remainder = divmod(N, k)
20+
21+
ans = []
22+
curr = head
23+
24+
for i in range(k):
25+
temp = curr
26+
for j in range(width + (i < remainder) - 1):
27+
if curr:
28+
curr = curr.next
29+
if curr:
30+
curr.next, curr = None, curr.next
31+
ans.append(temp)
32+
return ans

0 commit comments

Comments
 (0)