当前位置:网站首页>Flip the linked list ii[three ways to flip the linked list +dummyhead/ head insertion / tail insertion]
Flip the linked list ii[three ways to flip the linked list +dummyhead/ head insertion / tail insertion]
2022-06-30 22:00:00 【REN_ Linsen】
Flip list
Preface
There are two angles to flip the linked list , Three ways . The first angle , Flip the chain from the original list , Or the node pointing to the previous record , Recursive , Recyclable . Second angle , You don't have to work hard on the original list to complete it , use dummyHead Unified empty linked list / The first node operates , Insert the required nodes into the header ( Flip )/ The tail interpolation ( Find the nodes that meet the requirements ) Link nodes to the back .
One 、 Flip the middle part of the linked list

Two 、dummyHead& The first interpolation & The tail interpolation
1、 A scan + Flip list ( Another scan )
// Reverse a linked list II
public class ReverseBetween {
/* target: Flip the specified part of the linked list . Split the middle part directly , Flip again , Connect again . */
public ListNode reverseBetween(ListNode head, int left, int right) {
// Unified empty linked list / Operation of the first node .
ListNode dummyHead = new ListNode(0, head);
// Count
int cnt = 1;
// Generate intermediate chain
ListNode p = dummyHead;
ListNode begin = null, end = null, newHead = null, newTail = null;
while (p.next != null) {
if (cnt == left) {
begin = p;
newHead = p.next;
}
if (cnt == right) {
end = p.next.next;
newTail = p.next;
p.next.next = null;
break;
}
++cnt;
p = p.next;
}
// Flip list
reverseList(newHead);
// Splicing list
begin.next = newTail;
newHead.next = end;
// Back to the list
return dummyHead.next;
}
// except for, Recursion is also possible / Insert the head , common 3 An angle solution . One is the list , Class is to reproduce and combine nodes into a new chain .
private void reverseList(ListNode head) {
ListNode pre = null;
while (head != null) {
ListNode next = head.next;
head.next = pre;
pre = head;
head = 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;
}
}
}
2、 A scan & The first interpolation & The tail interpolation ( Advanced )
// Advanced : A scan .
class ReverseBetween2 {
/* target: Flip the specified part of the linked list . A scan , Turn over the matching place . */
public ListNode reverseBetween(ListNode head, int left, int right) {
// Unified empty linked list / Operation of the first node .
ListNode dummyHead = new ListNode();
ListNode newHead = dummyHead,tail = dummyHead;
// There is no need to flip / The nodes that need to be flipped are interpolated by tail / The first interpolation
// Count
int cnt = 1;
while (head != null) {
// Take nodes
ListNode node = head;
// Update next head
head = head.next;
// The first interpolation
if (left <= cnt && cnt <= right) {
node.next = newHead.next;
newHead.next = node;
// Determine whether to update the tail node .
if (tail == dummyHead) tail = tail.next;
}
// The tail interpolation
else {
// The tail interpolation , Need to update the header
newHead = node;
// Insert node
node.next = null;
tail.next = node;
tail = tail.next;
}
// Update went there .
++cnt;
}
// Back to the list
return dummyHead.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)dummy Use of nodes , Make it easy to operate the linked list , Cooperate with the upper insertion / Tail insertion , Can play with linked list operation .
reference
边栏推荐
- The programmer's girlfriend gave me a fatigue driving test
- Develop your own package
- 顺祝老吴的聚会
- 兴奋神经递质——谷氨酸与大脑健康
- Qsort function and Simulation Implementation of qsort function
- 5G 在智慧医疗中的需求
- Pytorch quantitative practice (1)
- Rethink healthy diet based on intestinal microbiome
- How to use data sets in machine learning?
- 《安富莱嵌入式周报》第270期:2022.06.13--2022.06.19
猜你喜欢

【MySQL入门】第一话 · 初入“数据库”大陆

用yml文件进行conda迁移环境时的报错小结

Look at the top 10 capabilities of alicloud cipu

USBCAN分析仪的配套CAN和CANFD综合测试软件LKMaster软件解决工程师CAN总线测试难题

A comprehensive understanding of gout: symptoms, risk factors, pathogenesis and management

Is machine learning suitable for girls?

Stinky tofu made by Grandma

1-2 安装并配置MySQL相关的软件

Installing jupyter notebook under Anaconda

SQL server extracts pure numbers from strings
随机推荐
Notes [introduction to JUC package and future]
ML&DL:機器學習和深度學習中超參數優化的簡介、評估指標、過擬合現象、常用的調參優化方法之詳細攻略
Anti leakage family photo in attack and defense drill
[untitled] first time to participate in CSDN activities
Qsort function and Simulation Implementation of qsort function
1-1 数据库的基本概念
Study summary of dynamic routing between capsules
1-14 express managed static resources
How to use data sets in machine learning?
Phoenix architecture: an architect's perspective
jupyterbook 清空控制台输出
Document Layout Analysis: A Comprehensive Survey 2019论文学习总结
【回溯】全排列 leetcode46
Turn: win others' follow with practical actions
1-10 根据不同的url响应客户端的内容
About, Qianxin detects code vulnerabilities and XSS series solves them
1-3 使用SQL管理数据库
谈谈数字化转型的几个关键问题
Which direction should college students choose to find jobs after graduation?
牛逼|珍藏多年的工具让我实现了带薪摸鱼自由