当前位置:网站首页>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
边栏推荐
- 【MySQL入门】第一话 · 初入“数据库”大陆
- 阿婆做的臭豆腐
- Summary of errors reported when using YML file to migrate CONDA environment
- 1-14 express托管静态资源
- Coredns modifying upstream
- 京东与腾讯续签三年战略合作协议;起薪涨至26万元,韩国三星SK争相加薪留住半导体人才;Firefox 102 发布|极客头条
- Stinky tofu made by Grandma
- 1-21 JSONP接口
- 请问,启牛证券开户,可以开户吗?安全吗?你想要的答案全在这里
- jenkins下载插件下载不了,解决办法
猜你喜欢
全面认识痛风:症状、风险因素、发病机理及管理
興奮神經遞質——穀氨酸與大腦健康
介绍一款|用于多组学整合和网络可视化分析的在线平台
Summary of errors reported when using YML file to migrate CONDA environment
机器学习中如何使用数据集?
Which direction should college students choose to find jobs after graduation?
机器学习适合女生学吗?
Error filesystemexception: /data/nodes/0/indices/gttxk-hntgkhacm-8n60jw/1/index/ es_ temp_ File: structure needs cleaning
Neurotransmetteurs excitateurs - glutamate et santé cérébrale
Clickhouse native monitoring item, system table description
随机推荐
Neurotransmetteurs excitateurs - glutamate et santé cérébrale
5g demand in smart medicine
Introduction and example of template method mode
程序员女友给我做了一个疲劳驾驶检测
A comprehensive understanding of gout: symptoms, risk factors, pathogenesis and management
机器学习工作要求研究生吗?
1-21 JSONP接口
Modify the name of the launched applet
1-17 express Middleware
The Three Musketeers: One for All!
Arcmap|assign values to different categories of IDS with the field calculator
Summary of errors reported when using YML file to migrate CONDA environment
1-11 创建线上文件服务
Introduction to go web programming: a probe into the excellent test library gocovey
1-2 install and configure MySQL related software
Go Web 编程入门: 一探优秀测试库 GoConvey
机器学习适合女生学吗?
[introduction to MySQL] the first conversation · first time in the "database" Mainland
Best wishes for Lao Wu's party
Pytorch quantitative perception training (qat) steps