当前位置:网站首页>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
边栏推荐
- 自定义注解实现校验
- Q弹松软的大号吐司,带来更舒服的睡眠
- Technical personnel advanced to draw a big picture of business, hand-in-hand teaching is coming
- Get screen height
- 06.论Redis持久化的几种方式
- 初识 Flutter 的绘图组件 — CustomPaint
- 5. TPM module initialization
- Oracle temporary table explanation
- 友盟(软件异常实时监听的好帮手:Crash)接入教程(有点基础的小白最易学的教程)
- [LeetCode] 爬楼梯【70】
猜你喜欢
随机推荐
[daily record] - bug encountered in BigDecimal division operation
Detailed analysis of operators i++ and ++i in JS, i++ and ++i
Oracle table creation and management
Analyze the maker education path integrating the essence of discipline
Using asyncio for concurrency
[learning notes] double + two points
酒旅板块复苏,亚朵继续上市梦,距离“新住宿经济第一股“还有多远?
The question of IBL precomputation is finally solved
Tcp/ip protocol stack, about TCP_ RST | TCP_ ACK correct attitude
For the first time in more than 20 years! CVPR best student thesis awarded to Chinese college students!
Some views on libco
Locking relay ydb-100, 100V
Metauniverse and virtual reality (II)
Q弹松软的大号吐司,带来更舒服的睡眠
[learning notes] structure
冲击继电器ZC-23/DC220V
DLS-42/6-4 DC110V双位置继电器
闭锁继电器YDB-100、100V
关于Unity一般的输入操作方式
A letter to 5000 fans!




![[go] go implements row column conversion of sets](/img/d9/6272e55b2d9c6b6fbdf2537773bb83.png)




