当前位置:网站首页>Parity linked list [two general directions of linked list operation]
Parity linked list [two general directions of linked list operation]
2022-07-01 01:18:00 【REN_ Linsen】
Two general directions of linked list operation
Preface
There are two ways to operate a linked list , One is simple operation , Just operate on the linked list ; One is very nice All-round operation , Set up dummy node , Can unify the empty linked list / The first node operates , Insert the nodes that meet the conditions with the header / Tail insertion dummy after .
One 、 Two general directions of linked list operation

Two 、dummy
package everyday.listNode;
// Odd and even list
public class OddEvenList {
/* target: Odd before even , Put even numbers first , Odd numbers come back . There are two ways to operate a linked list , One is simple operation , Just operate on the linked list ; One is very nice All-round operation , Set up dummy node , Can unify the empty linked list / The first node operates , Insert the nodes that meet the conditions with the header / Tail insertion dummy after . */
public ListNode oddEvenList(ListNode head) {
int cnt = 1;// Odd before even / First and then odd , Put the same kind of sequential tail interpolation together .
ListNode dummy1 = new ListNode();
ListNode p1 = dummy1;
ListNode dummy2 = new ListNode();
ListNode p2 = dummy2;
// Get parity linked list .
while (head != null) {
// Get the processed node .
ListNode node = head;
head = head.next;// Go to the next node first , OK, clean up the last node .
node.next = null;
if ((cnt & 1) == 1) {
p1.next = node;
p1 = p1.next;
}
if ((cnt & 1) == 0) {
p2.next = node;
p2 = p2.next;
}
// Identifies the next parity .
cnt = ++cnt & 1;
}
// Connect the two linked lists .
p1.next = dummy2.next;
return dummy1.next;
}
// 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) There are two ways to operate a linked list , General recommendation dummy Law . If it is a very simple linked list operation , You can also directly operate the linked list in place .
reference
[1] LeetCode Two general directions of linked list operation
边栏推荐
- Training discipline principle of robot programming
- 解析融合学科本质的创客教育路径
- Experiment 8 T-SQL, stored procedure
- Oracle table creation and management
- Two position relay st2-2l/ac220v
- Oracle temporary table explanation
- Inspire students' diversified thinking with steam Education
- 2022 is half way through. It's hard to make money
- 人穷志不短,穷学生也能玩转树莓派
- 孔乙己第一问之服务通信知多少?
猜你喜欢

Service

NE555 waveform generator handle tutorial NE555 internal structure (I)
![奇偶链表[链表操作的两种大方向]](/img/4e/ce860bc172bb75f456427ba26a7842.png)
奇偶链表[链表操作的两种大方向]

2021电赛F题openmv和K210调用openmv api巡线,完全开源。

Metauniverse and virtual reality (II)

初识 Flutter 的绘图组件 — CustomPaint

闭锁继电器YDB-100、100V

HDU 2488 A Knight's Journey(DFS)

Oracle data integrity

Get to know the drawing component of flutter - custompaint
随机推荐
What is the difference between Pipeline and Release Pipeline in azure devops?
C语言一点点(未来可会增加)
双链表:初始化 插入 删除 遍历
Ranger plug-in development (Part 2)
关于Unity一般的输入操作方式
Oracle temporary table explanation
【学习笔记】简单dp
[original] PLSQL index sorting optimization
集群与LVS介绍及原理解析
The real topic of the 11th provincial competition of Bluebridge cup 2020 - crop hybridization
Tcp/ip protocol stack, about TCP_ RST | TCP_ ACK correct attitude
双位置继电器DLS-5/2 DC220V
For the first time in more than 20 years! CVPR best student thesis awarded to Chinese college students!
关于VCTK数据集
Analyzing the wisdom principle in maker education practice
New content violation degree determination scana bad information monitoring capability update issue 5
TCP三次握手为什么不是两次或四次
The girlfriend said: if you want to understand the three MySQL logs, I will let you heiheihei!
Left join displays the specified value when the left join matching data is null
Docker 部署 MySQL 8