当前位置:网站首页>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
边栏推荐
- vim 常用快捷键
- Look at the top 10 capabilities of alicloud cipu
- 京东与腾讯续签三年战略合作协议;起薪涨至26万元,韩国三星SK争相加薪留住半导体人才;Firefox 102 发布|极客头条
- 1-20 预检请求
- 请问,启牛证券开户,可以开户吗?安全吗?你想要的答案全在这里
- 1-10 根据不同的url响应客户端的内容
- The Jenkins download Plug-in can't be downloaded. Solution
- Ml & DL: introduction to hyperparametric optimization in machine learning and deep learning, evaluation index, over fitting phenomenon, and detailed introduction to commonly used parameter adjustment
- 1-19 利用CORS解决接口跨域问题
- Five years after graduation, I wondered if I would still be so anxious if I hadn't taken the test
猜你喜欢

Uniapp routing uni simple router

Summary of errors reported when using YML file to migrate CONDA environment

Pytorch quantitative practice (1)

興奮神經遞質——穀氨酸與大腦健康

Error filesystemexception: /data/nodes/0/indices/gttxk-hntgkhacm-8n60jw/1/index/ es_ temp_ File: structure needs cleaning

京东与腾讯续签三年战略合作协议;起薪涨至26万元,韩国三星SK争相加薪留住半导体人才;Firefox 102 发布|极客头条

从PG15 XID64再次跳票说起

Best wishes for Lao Wu's party

Anaconda下安装Jupyter notebook

About, Qianxin detects code vulnerabilities and XSS series solves them
随机推荐
Document layout analysis: a comprehensive survey 2019 paper learning summary
[introduction to MySQL] the first conversation · first time in the "database" Mainland
VIM common shortcut keys
Uniapp life cycle / route jump
jupyter notebook/lab 切换conda环境
Arcmap|assign values to different categories of IDS with the field calculator
Gartner focuses on low code development in China how UNIPRO practices "differentiation"
Is Wu Enda's machine learning suitable for entry?
NCAT detailed introduction (Reprint)
Jupyterbook clear console output
Interesting plug-ins summary
1-16 路由的概念
Vite2 is compatible with lower versions of chrome (such as Sogou 80). Some grammars requiring higher versions are processed through polyfills
机器学习工作要求研究生吗?
微服务链路风险分析
阿婆做的臭豆腐
1-3 using SQL to manage databases
1-1 basic concepts of database
Sqlserver string type converted to decimal or integer type
1. Summary of wechat applet page Jump methods; 2. the navigateto stack does not jump to the tenth floor