Skip to content

Commit ad48bcb

Browse files
committed
feat: update leetcode solutions: 0725. Split Linked List in Parts
1 parent cfc5491 commit ad48bcb

File tree

4 files changed

+182
-27
lines changed

4 files changed

+182
-27
lines changed

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

+68-2
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,88 @@ root = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3
5454

5555
<!-- 这里可写通用的实现逻辑 -->
5656

57+
先遍历链表,统计结点总个数。
58+
59+
接着将链表拆分,`width` 表示每一部分至少含有的结点个数,而 `remainder` 表示前 `remainder` 部分,每一部分多出一个数。
60+
61+
然后遍历链表,依次拆出每一部分,添加到结果数组 `res` 即可。
62+
5763
<!-- tabs:start -->
5864

5965
### **Python3**
6066

6167
<!-- 这里可写当前语言的特殊实现逻辑 -->
6268

6369
```python
64-
70+
# Definition for singly-linked list.
71+
# class ListNode:
72+
# def __init__(self, x):
73+
# self.val = x
74+
# self.next = None
75+
76+
class Solution:
77+
def splitListToParts(self, root: ListNode, k: int) -> List[ListNode]:
78+
n, cur = 0, root
79+
while cur:
80+
n += 1
81+
cur = cur.next
82+
cur = root
83+
width, remainder = divmod(n, k)
84+
res = [None for _ in range(k)]
85+
for i in range(k):
86+
head = cur
87+
for j in range(width + (i < remainder) - 1):
88+
if cur:
89+
cur = cur.next
90+
if cur:
91+
cur.next, cur = None, cur.next
92+
res[i] = head
93+
return res
6594
```
6695

6796
### **Java**
6897

6998
<!-- 这里可写当前语言的特殊实现逻辑 -->
7099

71100
```java
72-
101+
/**
102+
* Definition for singly-linked list.
103+
* public class ListNode {
104+
* int val;
105+
* ListNode next;
106+
* ListNode(int x) { val = x; }
107+
* }
108+
*/
109+
class Solution {
110+
public ListNode[] splitListToParts(ListNode root, int k) {
111+
int n = 0;
112+
ListNode cur = root;
113+
while (cur != null) {
114+
++n;
115+
cur = cur.next;
116+
}
117+
// width 表示每一部分至少含有的结点个数
118+
// remainder 表示前 remainder 部分,每一部分多出一个数
119+
int width = n / k, remainder = n % k;
120+
ListNode[] res = new ListNode[k];
121+
cur = root;
122+
for (int i = 0; i < k; ++i) {
123+
ListNode head = cur;
124+
for (int j = 0; j < width + ((i < remainder) ? 1 : 0) - 1; ++j) {
125+
if (cur != null) {
126+
cur = cur.next;
127+
}
128+
}
129+
if (cur != null) {
130+
ListNode t = cur.next;
131+
cur.next = null;
132+
cur = t;
133+
}
134+
res[i] = head;
135+
}
136+
return res;
137+
}
138+
}
73139
```
74140

75141
### **...**

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

+60-2
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,71 @@ The input has been split into consecutive parts with size difference at most 1,
9393
### **Python3**
9494

9595
```python
96-
96+
# Definition for singly-linked list.
97+
# class ListNode:
98+
# def __init__(self, x):
99+
# self.val = x
100+
# self.next = None
101+
102+
class Solution:
103+
def splitListToParts(self, root: ListNode, k: int) -> List[ListNode]:
104+
n, cur = 0, root
105+
while cur:
106+
n += 1
107+
cur = cur.next
108+
cur = root
109+
width, remainder = divmod(n, k)
110+
res = [None for _ in range(k)]
111+
for i in range(k):
112+
head = cur
113+
for j in range(width + (i < remainder) - 1):
114+
if cur:
115+
cur = cur.next
116+
if cur:
117+
cur.next, cur = None, cur.next
118+
res[i] = head
119+
return res
97120
```
98121

99122
### **Java**
100123

101124
```java
102-
125+
/**
126+
* Definition for singly-linked list.
127+
* public class ListNode {
128+
* int val;
129+
* ListNode next;
130+
* ListNode(int x) { val = x; }
131+
* }
132+
*/
133+
class Solution {
134+
public ListNode[] splitListToParts(ListNode root, int k) {
135+
int n = 0;
136+
ListNode cur = root;
137+
while (cur != null) {
138+
++n;
139+
cur = cur.next;
140+
}
141+
int width = n / k, remainder = n % k;
142+
ListNode[] res = new ListNode[k];
143+
cur = root;
144+
for (int i = 0; i < k; ++i) {
145+
ListNode head = cur;
146+
for (int j = 0; j < width + ((i < remainder) ? 1 : 0) - 1; ++j) {
147+
if (cur != null) {
148+
cur = cur.next;
149+
}
150+
}
151+
if (cur != null) {
152+
ListNode t = cur.next;
153+
cur.next = null;
154+
cur = t;
155+
}
156+
res[i] = head;
157+
}
158+
return res;
159+
}
160+
}
103161
```
104162

105163
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
19
class Solution {
210
public ListNode[] splitListToParts(ListNode root, int k) {
11+
int n = 0;
12+
ListNode cur = root;
13+
while (cur != null) {
14+
++n;
15+
cur = cur.next;
16+
}
17+
// width 表示每一部分至少含有的结点个数
18+
// remainder 表示前 remainder 部分,每一部分多出一个数
19+
int width = n / k, remainder = n % k;
320
ListNode[] res = new ListNode[k];
4-
int n = getLength(root);
5-
int len = n / k, left = n % k;
6-
ListNode pre = null; // 记录链尾
7-
for (int i = 0; i < k && root != null; ++i) {
8-
res[i] = root;
9-
int step = len;
10-
if (left > 0) {
11-
--left;
12-
++step;
21+
cur = root;
22+
for (int i = 0; i < k; ++i) {
23+
ListNode head = cur;
24+
for (int j = 0; j < width + ((i < remainder) ? 1 : 0) - 1; ++j) {
25+
if (cur != null) {
26+
cur = cur.next;
27+
}
1328
}
14-
for (int j = 0; j < step; ++j) {
15-
pre = root;
16-
root = root.next;
29+
if (cur != null) {
30+
ListNode t = cur.next;
31+
cur.next = null;
32+
cur = t;
1733
}
18-
pre.next = null; // 断链
19-
}
20-
return res;
21-
}
22-
23-
private int getLength(ListNode root) {
24-
int res = 0;
25-
while (root != null) {
26-
++res;
27-
root = root.next;
34+
res[i] = head;
2835
}
2936
return res;
3037
}
31-
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def splitListToParts(self, root: ListNode, k: int) -> List[ListNode]:
9+
n, cur = 0, root
10+
while cur:
11+
n += 1
12+
cur = cur.next
13+
cur = root
14+
width, remainder = divmod(n, k)
15+
res = [None for _ in range(k)]
16+
for i in range(k):
17+
head = cur
18+
for j in range(width + (i < remainder) - 1):
19+
if cur:
20+
cur = cur.next
21+
if cur:
22+
cur.next, cur = None, cur.next
23+
res[i] = head
24+
return res

0 commit comments

Comments
 (0)