当前位置:网站首页>翻转链表II[翻转链表3种方式+dummyHead/头插法/尾插法]
翻转链表II[翻转链表3种方式+dummyHead/头插法/尾插法]
2022-06-30 20:16:00 【REN_林森】
前言
翻转链表有两个角度,三种方式。第一角度,从原链表上将链翻转,或叫指向前一个记录的节点,可递归,可循环。第二角度,完成不用在原链表上费力,采用dummyHead统一空链表/第一节点操作,将需要的节点以头插法(翻转)/尾插法(找符合要求的节点)将节点链接到其后。
一、翻转链表中间部分

二、dummyHead&头插法&尾插法
1、一次扫描 + 翻转链表(另一次扫描)
// 反转链表II
public class ReverseBetween {
/* target:将链表指定部分翻转。 直接把中间部分分割出来,再翻转,再连接。 */
public ListNode reverseBetween(ListNode head, int left, int right) {
// 统一空链表/第一个节点的操作。
ListNode dummyHead = new ListNode(0, head);
// 计数
int cnt = 1;
// 生成中间链
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;
}
// 翻转链表
reverseList(newHead);
// 拼接链表
begin.next = newTail;
newHead.next = end;
// 返回链表
return dummyHead.next;
}
// 除了for,还可递归/头插入,共3种角度解法。一类在链表上面该,类是将节点重现组合成新链。
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、一次扫描&头插法&尾插法(进阶)
// 进阶:一次扫描。
class ReverseBetween2 {
/* target:将链表指定部分翻转。 一次扫描,将符合的地方翻转即可。 */
public ListNode reverseBetween(ListNode head, int left, int right) {
// 统一空链表/第一个节点的操作。
ListNode dummyHead = new ListNode();
ListNode newHead = dummyHead,tail = dummyHead;
// 把不需要翻转/需要翻转的节点以尾插法/头插法
// 计数
int cnt = 1;
while (head != null) {
// 取节点
ListNode node = head;
// 更新下一个head
head = head.next;
// 头插法
if (left <= cnt && cnt <= right) {
node.next = newHead.next;
newHead.next = node;
// 判定是否需要更新尾巴节点。
if (tail == dummyHead) tail = tail.next;
}
// 尾插法
else {
// 尾插法,需要更新头
newHead = node;
// 插入节点
node.next = null;
tail.next = node;
tail = tail.next;
}
// 更新走到那里了。
++cnt;
}
// 返回链表
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;
}
}
}
总结
1)dummy节点的使用,让操作链表方便起来,配合上头插法/尾插入,可玩转链表操作。
参考文献
[1] LeetCode 翻转链表II
边栏推荐
- Heartbeat uses NFS to make MySQL highly available based on CRM
- B_QuRT_User_Guide(34)
- Lumiprobe protein quantitation - qudye Protein Quantitation Kit
- Lvalue reference and lvalue reference
- Black apple server system installation tutorial, black apple installation tutorial, teach you how to install black apple in detail [easy to understand]
- 断点续传和下载原理分析
- 杰理之关于长按开机检测抬起问题【篇】
- QT qstringlist usage
- NLP 论文领读|文本生成模型退化怎么办?SimCTG 告诉你答案
- 亚马逊在阿拉伯联合酋长国限制LGBTQ相关的搜索和产品销售
猜你喜欢

Lumiprobe染料酰肼丨BDP FL 酰肼方案

Huffman tree (I) basic concept and C language implementation

1. Introduction to generating countermeasures network

Solve the problems of Devops landing in complex environment with various tools with full stack and full function solutions

Lumiprobe无铜点击化学解决方案

NLP 论文领读|文本生成模型退化怎么办?SimCTG 告诉你答案

Halcon知识:盘点一下计量对象【1】

二叉查找树(一) - 概念与C语言实现

杰理之触摸按键识别流程【篇】

Huffman Tree (1) Basic Concept and C - language Implementation
随机推荐
BioVendor sRAGE Elisa试剂盒测试原理和注意事项
大神詳解開源 BUFF 增益攻略丨直播
Lumiprobe dye hydrazide - BDP FL hydrazide solution
Common questions and answering skills of project manager interview
Tensorflow2.4 implementation of repvgg
By analyzing more than 7million R & D needs, it is found that these eight programming languages are the most needed by the industry
为什么vscode用久了电脑速度变慢?
Lumiprobe核酸定量丨QuDye dsDNA BR 检测试剂盒
SQL必需掌握的100个重要知识点:创建和操纵表
好高的佣金,《新程序员》合伙人计划来袭,人人皆可参与
[1175. prime number arrangement]
Lumiprobe蛋白质定量丨QuDye 蛋白定量试剂盒
基于开源流批一体数据同步引擎ChunJun数据还原—DDL解析模块的实战分享
How do I get the largest K massive data
Lumiprobe protein quantitation - qudye Protein Quantitation Kit
信息学奥赛一本通 1362:家庭问题(family)
杰理之触摸按键识别流程【篇】
B_QuRT_User_Guide(31)
QT qstringlist usage
Introduction to neural network (Part 1)