725:分隔链表
This commit is contained in:
parent
3be18188bc
commit
ff1d6a0887
100
src/main/java/leetcode/editor/cn/SplitLinkedListInParts.java
Normal file
100
src/main/java/leetcode/editor/cn/SplitLinkedListInParts.java
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
//给定一个头结点为 root 的链表, 编写一个函数以将链表分隔为 k 个连续的部分。
|
||||||
|
//
|
||||||
|
// 每部分的长度应该尽可能的相等: 任意两部分的长度差距不能超过 1,也就是说可能有些部分为 null。
|
||||||
|
//
|
||||||
|
// 这k个部分应该按照在链表中出现的顺序进行输出,并且排在前面的部分的长度应该大于或等于后面的长度。
|
||||||
|
//
|
||||||
|
// 返回一个符合上述规则的链表的列表。
|
||||||
|
//
|
||||||
|
// 举例: 1->2->3->4, k = 5 // 5 结果 [ [1], [2], [3], [4], null ]
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:
|
||||||
|
//root = [1, 2, 3], k = 5
|
||||||
|
//输出: [[1],[2],[3],[],[]]
|
||||||
|
//解释:
|
||||||
|
//输入输出各部分都应该是链表,而不是数组。
|
||||||
|
//例如, 输入的结点 root 的 val= 1, root.next.val = 2, \root.next.next.val = 3, 且 root.ne
|
||||||
|
//xt.next.next = null。
|
||||||
|
//第一个输出 output[0] 是 output[0].val = 1, output[0].next = null。
|
||||||
|
//最后一个元素 output[4] 为 null, 它代表了最后一个部分为空链表。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:
|
||||||
|
//root = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3
|
||||||
|
//输出: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
|
||||||
|
//解释:
|
||||||
|
//输入被分成了几个连续的部分,并且每部分的长度相差不超过1.前面部分的长度大于等于后面部分的长度。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// root 的长度范围: [0, 1000].
|
||||||
|
// 输入的每个节点的大小范围:[0, 999].
|
||||||
|
// k 的取值范围: [1, 50].
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Related Topics 链表
|
||||||
|
// 👍 137 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import com.code.leet.entiy.ListNode;
|
||||||
|
|
||||||
|
//725:分隔链表
|
||||||
|
public class SplitLinkedListInParts{
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new SplitLinkedListInParts().new Solution();
|
||||||
|
}
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
/**
|
||||||
|
* Definition for singly-linked list.
|
||||||
|
* public class ListNode {
|
||||||
|
* int val;
|
||||||
|
* ListNode next;
|
||||||
|
* ListNode(int x) { val = x; }
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
class Solution {
|
||||||
|
public ListNode[] splitListToParts(ListNode root, int k) {
|
||||||
|
ListNode temp = root;
|
||||||
|
int size =0;
|
||||||
|
while (temp != null) {
|
||||||
|
size++;
|
||||||
|
temp = temp.next;
|
||||||
|
}
|
||||||
|
int length = size / k;
|
||||||
|
int add1 = size % k;
|
||||||
|
temp = root;
|
||||||
|
ListNode[] listNodes = new ListNode[k];
|
||||||
|
for (int i = 0; i < k; ++i) {
|
||||||
|
listNodes[i] = temp;
|
||||||
|
ListNode head = null;
|
||||||
|
for (int j = 0; j < length + (i < add1 ? 1 : 0); ++j) {
|
||||||
|
head = temp;
|
||||||
|
temp = temp.next;
|
||||||
|
if (temp == null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (head != null) {
|
||||||
|
head.next = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return listNodes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
45
src/main/java/leetcode/editor/cn/SplitLinkedListInParts.md
Normal file
45
src/main/java/leetcode/editor/cn/SplitLinkedListInParts.md
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<p>给定一个头结点为 <code>root</code> 的链表, 编写一个函数以将链表分隔为 <code>k</code> 个连续的部分。</p>
|
||||||
|
|
||||||
|
<p>每部分的长度应该尽可能的相等: 任意两部分的长度差距不能超过 1,也就是说可能有些部分为 null。</p>
|
||||||
|
|
||||||
|
<p>这k个部分应该按照在链表中出现的顺序进行输出,并且排在前面的部分的长度应该大于或等于后面的长度。</p>
|
||||||
|
|
||||||
|
<p>返回一个符合上述规则的链表的列表。</p>
|
||||||
|
|
||||||
|
<p>举例: 1->2->3->4, k = 5 // 5 结果 [ [1], [2], [3], [4], null ]</p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>
|
||||||
|
root = [1, 2, 3], k = 5
|
||||||
|
<strong>输出:</strong> [[1],[2],[3],[],[]]
|
||||||
|
<strong>解释:</strong>
|
||||||
|
输入输出各部分都应该是链表,而不是数组。
|
||||||
|
例如, 输入的结点 root 的 val= 1, root.next.val = 2, \root.next.next.val = 3, 且 root.next.next.next = null。
|
||||||
|
第一个输出 output[0] 是 output[0].val = 1, output[0].next = null。
|
||||||
|
最后一个元素 output[4] 为 null, 它代表了最后一个部分为空链表。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>
|
||||||
|
root = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3
|
||||||
|
<strong>输出:</strong> [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
|
||||||
|
<strong>解释:</strong>
|
||||||
|
输入被分成了几个连续的部分,并且每部分的长度相差不超过1.前面部分的长度大于等于后面部分的长度。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>root</code> 的长度范围: <code>[0, 1000]</code>.</li>
|
||||||
|
<li>输入的每个节点的大小范围:<code>[0, 999]</code>.</li>
|
||||||
|
<li><code>k</code> 的取值范围: <code>[1, 50]</code>.</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
<div><div>Related Topics</div><div><li>链表</li></div></div>\n<div><li>👍 137</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user