当前位置:网站首页>Split the linked list [take next first and then cut the linked list to prevent chain breakage]
Split the linked list [take next first and then cut the linked list to prevent chain breakage]
2022-07-01 01:18:00 【REN_ Linsen】
Split the list
Preface
One of the core problems of linked list operation , It's a broken chain , The continuous chain needs to point to the following passive nodes with pointer variables first .
One 、 Split the list

Two 、 First next Then cut off the linked list
package everyday.listNode;
// Split the list
public class SplitListToParts {
/* target: Given the list and how many shares , Split the list . As long as you can determine how many nodes each linked list is divided into , Traversing the linked list , You can do the segmentation . How to determine how many nodes each linked list is divided into ? Chain length len Take the remainder of the number of copies & Quotient of copies . Remainder , In order to get the first few nodes, you can divide them into one more node ; Take business , In order to determine the number of basic nodes assigned to a linked list . */
public ListNode[] splitListToParts(ListNode head, int k) {
// return k A linked list .
ListNode[] ans = new ListNode[k];
// Traversing the linked list , Find the length of the linked list .
int len = 0;
ListNode p = head;
while (p != null && (p = p.next) == p) ++len;
// Remainder , In order to get the first few nodes, you can divide them into one more node .
int mod = len % k;
// Take business , In order to determine the number of basic nodes assigned to a linked list .
int val = len / k;
// Start split , And the assignment .
int idx = 0;
p = head;
while (idx < k) {
// If there is no divisible situation , Then each of the following can only be divided into null.
if (p == null) {
ans[idx++] = null;
continue;
}
// Can be divided into non null node , Assign the initial node to the array .
ans[idx++] = p;
// Divide the linked list according to the current number of nodes that can be divided .
for (int i = 1;i < (mod != 0 ? 1 : 0) + val;i++) p = p.next;
// Get the next node , And cut off the linked list .
ListNode next = p.next;
p.next = null;
// Set the initial node of the new linked list , Start the next round of assignment .
p = next;
// mod It indicates how many linked lists can be assigned to an additional node , Until the score is finished .
if (mod > 0) --mod;
}
// Return the filled linked list array .
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;
}
}
}
summary
1) If you don't want to break the chain, you need to point to the following passive nodes with pointer variables .
reference
边栏推荐
- History of deep learning
- 【学习笔记】倍增 + 二分
- (学习力+思考力) x 行动力,技术人成长的飞轮效应总结
- [LeetCode] 两数之和【1】
- Unhandled Exception: MissingPluginException(No implementation found for method launch on channel)
- [daily record] - bug encountered in BigDecimal division operation
- Analyze the maker education path integrating the essence of discipline
- 【原创】 PLSQL 索引排序优化
- 解读创客教育所蕴含的科技素养
- Listview in flutter application development
猜你喜欢

Practical shell knowledge

Unhandled Exception: MissingPluginException(No implementation found for method launch on channel)

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

ESP8266 RC522

TCP三次握手为什么不是两次或四次

【原创】 PLSQL 索引排序优化

Shift operators

K210 site helmet

Green, green the reed. dew and frost gleam.

NE555 waveform generator handle tutorial NE555 internal structure (I)
随机推荐
Q弹松软的大号吐司,带来更舒服的睡眠
Impact relay zc-23/dc220v
ESP8266 RC522
mustache语法
初识 Flutter 的绘图组件 — CustomPaint
06.论Redis持久化的几种方式
Win11安装redis 数据库以及redis desktop manager的下载
解析创客教育实践中的智慧原理
5. TPM module initialization
【网络丢包,网络延迟?这款神器帮你搞定所有!】
C#生成putty格式的ppk文件(支持passphrase)
探索互联网时代STEAM教育创新之路
Exercise and health
魔王冷饭||#101 魔王解惑数量多与质量;员工管理;高考志愿填报;游戏架构设计
人穷志不短,穷学生也能玩转树莓派
双位置继电器DLS-5/2 DC220V
Oracle table creation and management
Koa koa-combine-routers 分路由管理
Dls-42/6-4 dc110v double position relay
Pytorch installs and uses GPU acceleration