当前位置:网站首页>分割链表[先取next再斩断链表防止断链]
分割链表[先取next再斩断链表防止断链]
2022-07-01 00:39:00 【REN_林森】
前言
链表操作的核心问题之一,就是断链,不断链就需要把后面的被动节点先用指针变量指着。
一、分割链表

二、先取next再斩断链表
package everyday.listNode;
// 分割链表
public class SplitListToParts {
/* target:给定链表和分成多少份,分割链表。 只要能确定每个链表分割多少个节点,遍历链表,就可以完成分割。 如何确定每个链表分割多少节点?链表长度len 对份数取余 & 对份数取商。 取余,为了得到前几个可以多分一个节点;取商,为了确定一个链表分到的基本节点个数。 */
public ListNode[] splitListToParts(ListNode head, int k) {
// 返回k个链表。
ListNode[] ans = new ListNode[k];
// 遍历链表,求得链表长度。
int len = 0;
ListNode p = head;
while (p != null && (p = p.next) == p) ++len;
// 取余,为了得到前几个可以多分一个节点。
int mod = len % k;
// 取商,为了确定一个链表分到的基本节点个数。
int val = len / k;
// 开始分割,并赋值。
int idx = 0;
p = head;
while (idx < k) {
// 如果已经没有可分割的情况,则后面的每一个只能分到null.
if (p == null) {
ans[idx++] = null;
continue;
}
// 能分到非null节点,把初始节点赋值到数组中。
ans[idx++] = p;
// 根据当前能分到的节点数来分割链表。
for (int i = 1;i < (mod != 0 ? 1 : 0) + val;i++) p = p.next;
// 拿到下一个节点,并斩断链表。
ListNode next = p.next;
p.next = null;
// 设置好新链表的初始节点,开始下一轮的赋值。
p = next;
// mod表示前面有多少链表可以分到额外的一个节点,直至分完。
if (mod > 0) --mod;
}
// 返回填充好的链表数组。
return ans;
}
// Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
}
总结
1)不想断链就需要把后面的被动节点先用指针变量指着。
参考文献
[1] LeetCode 分割链表
边栏推荐
- Interface documentation system - Yapi
- SAP ui5 beginner tutorial 19 - SAP ui5 data types and complex data binding
- What if the disk of datanode is full?
- What is the difference between Pipeline and Release Pipeline in azure devops?
- CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
- The principle of journal node
- Docsify building a personal minimalist knowledge warehouse
- A letter to 5000 fans!
- K210门禁毕设
- Unit test concept and purpose
猜你喜欢

Two-stage RO: part 1

Vnctf 2022 cm CM1 re reproduction

Packing and unpacking of C #

剑指 Offer 18. 删除链表的节点

New content violation degree determination scana bad information monitoring capability update issue 5

DLS-20型双位置继电器 220VDC

Q弹松软的大号吐司,带来更舒服的睡眠

Cmu15445 (fall 2019) project 1 - buffer pool details

Docsify building a personal minimalist knowledge warehouse

Exercises on recursion in C language
随机推荐
优质的水泵 SolidWorks模型素材推荐,不容错过
深度学习的历史
Authentication principle of Ranger plug-in
HDU 2488 A Knight's Journey(DFS)
History of deep learning
Detailed analysis of operators i++ and ++i in JS, i++ and ++i
【日常记录】——对BigDecimal除法运算时遇到的Bug
ArrayList分析1-循环、扩容、版本
Two-stage RO: part 1
剑指 Offer 19. 正则表达式匹配
Gavin's insight on the transformer live broadcast course - rasa project's actual banking financial BOT Intelligent Business Dialogue robot system startup, language understanding, dialogue decision-mak
leetcode 474. Ones and Zeroes 一和零(中等)
女朋友说:你要搞懂了MySQL三大日志,我就让你嘿嘿嘿!
High quality pump SolidWorks model material recommended, not to be missed
对libco的一点看法
K210门禁毕设
The quantity and quality of the devil's cold rice 101; Employee management; College entrance examination voluntary filling; Game architecture design
What is product thinking
闭锁继电器YDB-100、100V
Redis based distributed lock