Skip to content

Commit 583ebf7

Browse files
authored
feat: add solutions to lc problem: No.0725 (doocs#3323)
No.0725.Split Linked List in Parts
1 parent 1b7f060 commit 583ebf7

File tree

7 files changed

+456
-123
lines changed

7 files changed

+456
-123
lines changed

solution/0700-0799/0725.Split Linked List in Parts/README.md

+159-41
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ tags:
6060

6161
<!-- solution:start -->
6262

63-
### 方法一
63+
### 方法一:模拟
64+
65+
我们先遍历链表,得到链表的长度 $n$,然后我们计算出平均长度 $\textit{cnt} = \lfloor \frac{n}{k} \rfloor$ 和余数 $\textit{mod} = n \bmod k$。那么对于前 $\textit{mod}$ 个部分,每个部分的长度为 $\textit{cnt} + 1$,其余部分的长度为 $\textit{cnt}$。
66+
67+
接下来,我们只需要遍历链表,将链表分割成 $k$ 个部分即可。
68+
69+
时间复杂度 $O(n)$,空间复杂度 $O(k)$。其中 $n$ 为链表的长度。
6470

6571
<!-- tabs:start -->
6672

@@ -69,29 +75,32 @@ tags:
6975
```python
7076
# Definition for singly-linked list.
7177
# class ListNode:
72-
# def __init__(self, x):
73-
# self.val = x
74-
# self.next = None
75-
76-
78+
# def __init__(self, val=0, next=None):
79+
# self.val = val
80+
# self.next = next
7781
class Solution:
78-
def splitListToParts(self, root: ListNode, k: int) -> List[ListNode]:
79-
n, cur = 0, root
82+
def splitListToParts(
83+
self, head: Optional[ListNode], k: int
84+
) -> List[Optional[ListNode]]:
85+
n = 0
86+
cur = head
8087
while cur:
8188
n += 1
8289
cur = cur.next
83-
cur = root
84-
width, remainder = divmod(n, k)
85-
res = [None for _ in range(k)]
90+
cnt, mod = divmod(n, k)
91+
ans = [None] * k
92+
cur = head
8693
for i in range(k):
87-
head = cur
88-
for j in range(width + (i < remainder) - 1):
89-
if cur:
90-
cur = cur.next
91-
if cur:
92-
cur.next, cur = None, cur.next
93-
res[i] = head
94-
return res
94+
if cur is None:
95+
break
96+
ans[i] = cur
97+
m = cnt + int(i < mod)
98+
for _ in range(1, m):
99+
cur = cur.next
100+
nxt = cur.next
101+
cur.next = None
102+
cur = nxt
103+
return ans
95104
```
96105

97106
#### Java
@@ -102,38 +111,147 @@ class Solution:
102111
* public class ListNode {
103112
* int val;
104113
* ListNode next;
105-
* ListNode(int x) { val = x; }
114+
* ListNode() {}
115+
* ListNode(int val) { this.val = val; }
116+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
106117
* }
107118
*/
108119
class Solution {
109-
public ListNode[] splitListToParts(ListNode root, int k) {
120+
public ListNode[] splitListToParts(ListNode head, int k) {
110121
int n = 0;
111-
ListNode cur = root;
112-
while (cur != null) {
122+
for (ListNode cur = head; cur != null; cur = cur.next) {
113123
++n;
114-
cur = cur.next;
115124
}
116-
// width 表示每一部分至少含有的结点个数
117-
// remainder 表示前 remainder 部分,每一部分多出一个数
118-
int width = n / k, remainder = n % k;
119-
ListNode[] res = new ListNode[k];
120-
cur = root;
121-
for (int i = 0; i < k; ++i) {
122-
ListNode head = cur;
123-
for (int j = 0; j < width + ((i < remainder) ? 1 : 0) - 1; ++j) {
124-
if (cur != null) {
125-
cur = cur.next;
126-
}
125+
int cnt = n / k, mod = n % k;
126+
ListNode[] ans = new ListNode[k];
127+
ListNode cur = head;
128+
for (int i = 0; i < k && cur != null; ++i) {
129+
ans[i] = cur;
130+
int m = cnt + (i < mod ? 1 : 0);
131+
for (int j = 1; j < m; ++j) {
132+
cur = cur.next;
127133
}
128-
if (cur != null) {
129-
ListNode t = cur.next;
130-
cur.next = null;
131-
cur = t;
134+
ListNode nxt = cur.next;
135+
cur.next = null;
136+
cur = nxt;
137+
}
138+
return ans;
139+
}
140+
}
141+
```
142+
143+
#### C++
144+
145+
```cpp
146+
/**
147+
* Definition for singly-linked list.
148+
* struct ListNode {
149+
* int val;
150+
* ListNode *next;
151+
* ListNode() : val(0), next(nullptr) {}
152+
* ListNode(int x) : val(x), next(nullptr) {}
153+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
154+
* };
155+
*/
156+
class Solution {
157+
public:
158+
vector<ListNode*> splitListToParts(ListNode* head, int k) {
159+
int n = 0;
160+
for (ListNode* cur = head; cur != nullptr; cur = cur->next) {
161+
++n;
162+
}
163+
int cnt = n / k, mod = n % k;
164+
vector<ListNode*> ans(k, nullptr);
165+
ListNode* cur = head;
166+
for (int i = 0; i < k && cur != nullptr; ++i) {
167+
ans[i] = cur;
168+
int m = cnt + (i < mod ? 1 : 0);
169+
for (int j = 1; j < m; ++j) {
170+
cur = cur->next;
132171
}
133-
res[i] = head;
172+
ListNode* nxt = cur->next;
173+
cur->next = nullptr;
174+
cur = nxt;
175+
}
176+
return ans;
177+
}
178+
};
179+
```
180+
181+
#### Go
182+
183+
```go
184+
/**
185+
* Definition for singly-linked list.
186+
* type ListNode struct {
187+
* Val int
188+
* Next *ListNode
189+
* }
190+
*/
191+
func splitListToParts(head *ListNode, k int) []*ListNode {
192+
n := 0
193+
for cur := head; cur != nil; cur = cur.Next {
194+
n++
195+
}
196+
197+
cnt := n / k
198+
mod := n % k
199+
ans := make([]*ListNode, k)
200+
cur := head
201+
202+
for i := 0; i < k && cur != nil; i++ {
203+
ans[i] = cur
204+
m := cnt
205+
if i < mod {
206+
m++
207+
}
208+
for j := 1; j < m; j++ {
209+
cur = cur.Next
210+
}
211+
next := cur.Next
212+
cur.Next = nil
213+
cur = next
214+
}
215+
216+
return ans
217+
}
218+
```
219+
220+
#### TypeScript
221+
222+
```ts
223+
/**
224+
* Definition for singly-linked list.
225+
* class ListNode {
226+
* val: number
227+
* next: ListNode | null
228+
* constructor(val?: number, next?: ListNode | null) {
229+
* this.val = (val===undefined ? 0 : val)
230+
* this.next = (next===undefined ? null : next)
231+
* }
232+
* }
233+
*/
234+
235+
function splitListToParts(head: ListNode | null, k: number): Array<ListNode | null> {
236+
let n = 0;
237+
for (let cur = head; cur !== null; cur = cur.next) {
238+
n++;
239+
}
240+
const cnt = (n / k) | 0;
241+
const mod = n % k;
242+
const ans: Array<ListNode | null> = Array(k).fill(null);
243+
let cur = head;
244+
for (let i = 0; i < k && cur !== null; i++) {
245+
ans[i] = cur;
246+
let m = cnt + (i < mod ? 1 : 0);
247+
for (let j = 1; j < m; j++) {
248+
cur = cur.next!;
134249
}
135-
return res;
250+
let next = cur.next;
251+
cur.next = null;
252+
cur = next;
136253
}
254+
return ans;
137255
}
138256
```
139257

0 commit comments

Comments
 (0)