当前位置:网站首页>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
边栏推荐
猜你喜欢

Two-stage RO: part 1

魔王冷饭||#101 魔王解惑数量多与质量;员工管理;高考志愿填报;游戏架构设计

Win11安装redis 数据库以及redis desktop manager的下载

C # generates PPK files in putty format (supports passphrase)

Training discipline principle of robot programming

K210工地安全帽

What if the disk of datanode is full?

Double position relay dls-5/2 dc220v

Impact relay zc-23/dc220v

【网络丢包,网络延迟?这款神器帮你搞定所有!】
随机推荐
[original] PLSQL index sorting optimization
ASCII、Unicode、GBK、UTF-8之间的关系
写给 5000 粉丝的一封信!
Some views on libco
Web compatibility testing of software testing
History of deep learning
Oracle temporary table explanation
CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
Unhandled Exception: MissingPluginException(No implementation found for method launch on channel)
For the first time in more than 20 years! CVPR best student thesis awarded to Chinese college students!
The communication mechanism and extension of Supervisor
The girlfriend said: if you want to understand the three MySQL logs, I will let you heiheihei!
DLS-20型双位置继电器 220VDC
初识 Flutter 的绘图组件 — CustomPaint
High quality pump SolidWorks model material recommended, not to be missed
[go] go implements row column conversion of sets
[learning notes] double + two points
【原创】 PLSQL 索引排序优化
NE555 waveform generator handle tutorial NE555 internal structure (I)
New content violation degree determination scana bad information monitoring capability update issue 5