当前位置:网站首页>【BM2 链表内指定区间反转】
【BM2 链表内指定区间反转】
2022-07-26 06:08:00 【安河桥畔】
链表内指定区间反转
题目来源
牛客网:链表内指定区间反转
题目描述
将一个节点数为 size 链表 m 位置到 n 位置之间的区间反转,要求时间复杂度 O(n)O(n),空间复杂度 O(1)O(1)。
例如:
给出的链表为 1→2→3→4→5→NULL,m=2,n=4,
返回 1→4→3→2→5→NULL.
数据范围: 链表长度 0 <size≤1000,0 < m≤n≤size,链表中每个节点的值满足∣val∣≤1000
要求:时间复杂度 O(n)O(n) ,空间复杂度 O(n)O(n)
进阶:时间复杂度 O(n)O(n),空间复杂度 O(1)O(1)
示例1
输入
{1,2,3,4,5},2,4
返回值
{1,4,3,2,5}
示例2
输入
{5},1,1
输出
{5}
思路分析
- 定义一个虚拟的头节点,防止链表的头节点也被逆置
- 找到要反转的区间的起始位置及其前驱节点
- 从起始位置按单链表逆置的方法进行逆置
- 将逆置的部分与原链表进行拼接
- 返回虚拟的头结点的下一个节点
代码展示
ListNode* reverseBetween(ListNode* head, int m, int n) {
if (head == NULL)
{
return NULL;
}
//头节点也有可能会被反转,定义一个虚拟的头节点
ListNode* myhead = new ListNode(0);//申请新节点
myhead->next = head;//新节点与原来的头节点连接
ListNode* cur = myhead;
for (int i = 0; i < m - 1; i++)
{
cur = cur->next;
}
ListNode* prev = cur;//开始反转点的前一个节点
ListNode* start = prev->next;//开始反转的节点
cur = start->next;
ListNode* before = start;
ListNode* next = nullptr;
while (m < n)
{
next = cur->next;
cur->next = before;
before = cur;
cur = next;
m++;
}
prev->next = before;//prev的next指向反转后的起点
start->next = cur;//start反转后成为end,所以start的next指向原来结尾的下一个节点
return myhead->next;
}
};
边栏推荐
- [2023 Jerry technology approval test questions in advance] ~ questions and reference answers
- 二叉排序树(BST) ~
- Using dynamic libraries in VS
- Ganglia安装部署流程
- Ganglia installation and deployment process
- 2022年下半年系统集成项目管理工程师(软考中级)报名条件
- 知识沉淀一:架构师是做什么?解决了什么问题
- Mysql45 talking about global lock, table lock and row lock
- 金仓数据库 KingbaseES SQL 语言参考手册 (6. 表达式)
- Convolutional neural network (II) - deep convolutional network: case study
猜你喜欢

Embedded sharing collection 15

数据库sql语言实战

How to divide the disks under the devices and drives in win10 new computer

Lemon class automatic learning after all

JDBC streaming query and cursor query
![[the most complete and detailed] ten thousand words explanation: activiti workflow engine](/img/4c/2e43aef33c6ecd67d40730d78d29dc.png)
[the most complete and detailed] ten thousand words explanation: activiti workflow engine

Mysql45 talking about global lock, table lock and row lock

Kingbasees SQL language reference manual of Jincang database (7. Conditional expression)

Cdga | how to build data asset catalogue?
![[Oracle SQL] calculate year-on-year and month on month (column to row offset)](/img/ee/59d050e03c2a4ba04de57df1322283.png)
[Oracle SQL] calculate year-on-year and month on month (column to row offset)
随机推荐
Day110. Shangyitong: gateway integration, hospital scheduling management: Department list, statistics based on date, scheduling details
Xiao He shows his sharp corners and says hello to flutter app
Talking about the practice of software defect management
Understanding the mathematical essence of machine learning
[the most complete and detailed] ten thousand words explanation: activiti workflow engine
二叉树的性质 ~
分布式 | 实战:将业务从 MyCAT 平滑迁移到 dble
Servlet filter details
日志收集分析平台搭建-1-环境准备
[2023 Jerry technology approval test questions in advance] ~ questions and reference answers
em和rem
光量子里程碑:6分钟内解决3854个变量问题
平衡二叉树(AVL) ~
[paper notes] anti packet loss joint coding for network speech steganography
Project topic selection reference
[highly available MySQL solution] centos7 configures MySQL master-slave replication
[(SV & UVM) knowledge points encountered in written interview] ~ phase mechanism
Qu Weihai, chairman and CEO of Xinyi interactive, adheres to mutual benefit and win-win results, and Qu Weihai promotes enterprise development
Amd zen4 game God u reached 208mb cache within this year, which is unprecedented
Detailed explanation of the whole process of coding's pressure measurement operation