当前位置:网站首页>链表内指定区间反转
链表内指定区间反转
2022-07-27 02:50:00 【利刃Cc】
描述
将一个节点数为 size 链表 m 位置到 n 位置之间的区间反转,要求时间复杂度 O(n)O(n),空间复杂度 O(1)O(1)。
例如:
给出的链表为 1→ 2→ 3 → 4→ 5 → NULL, m=2,n=4m=2,n=4,
返回 1→ 4→ 3 → 2 → 5 → NULL。
要求:时间复杂度 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}
方法:头插法迭代
在学会了BM1.反转链表之后,要解决这个问题就很简单了,前一题是整个链表反转,这一题是部分反转,这上一题就是这道题的前置问题啊。那我们肯定是要先找到了第m个位置才能开始反转链表,而反转的部分就是从第m个位置到第n个位置,反转这部分的时候就参照BM1.反转链表
具体做法:
- step 1:我们可以在链表前加一个表头,后续返回时去掉就好了,因为如果要从链表头的位置开始反转,在多了一个表头的情况下就能保证第一个节点永远不会反转,不会到后面去。
- step 2:使用两个指针,一个指向当前节点,一个指向前序节点。
- step 3:依次遍历链表,到第m个的位置。
- step 4:对于从m到n这些个位置的节点,依次断掉指向后续的指针,反转指针方向。
- step 5:返回时去掉我们添加的表头。
/** * struct ListNode { * int val; * struct ListNode *next; * }; */
class Solution {
public:
/** * * @param head ListNode类 * @param m int整型 * @param n int整型 * @return ListNode类 */
ListNode* reverseBetween(ListNode* head, int m, int n) {
//创建一个新表头
ListNode* newhead = new ListNode(-1);
newhead->next = head;
ListNode* pre = newhead;
ListNode* cur = head;
for(int i = 1; i < m; ++i) //找到m的位置
{
pre = cur;
cur = cur->next;
}
for(int i = m; i < n; ++i) //从m反转到n
{
ListNode* next = cur->next;
cur->next = next->next;
next->next = pre->next;
pre->next = next;
}
return newhead->next;
}
};
边栏推荐
- Article main content extraction software [based on NLP technology]
- There is no problem reading from flick CDC to mysql8 and mysql5. What should I do?
- H. 265 web player easyplayer's method of opening video to the public
- A. YES or YES?
- Redis database, which can be understood by zero foundation Xiaobai, is easy to learn and use!
- VR全景人淘金“小心机”(上)
- Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
- A. Round Down the Price
- Want to get the Apache official domain name mailbox? Exclusive interview with Apache linkis five new committers to tell you how to do it
- PSINS工具箱中轨迹生成工具详细解析
猜你喜欢

Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community

VR全景人淘金“小心机”(上)

Chapter 4 决策树和随机森林

《MySQL》认识MySQL与计算机基础知识

Subject 3: Jinan Zhangqiu line 3

Redis database, which can be understood by zero foundation Xiaobai, is easy to learn and use!

Feitengtengrui d2000 won the "top ten hard core technologies" award of Digital China

2022危险化学品生产单位安全生产管理人员考试题模拟考试题库模拟考试平台操作

Message queue learning -- Concepts

Restful Fast Request 2022.2.2发布,支持批量导出文档
随机推荐
酷雷曼VR全景为你铺设创业之路
11. Zuul routing gateway
There is no problem reading from flick CDC to mysql8 and mysql5. What should I do?
Interview question: the difference between three instantiated objects in string class
想要获得 Apache 官方域名邮箱吗?专访 Apache Linkis 五位新晋 Committer告诉你怎么做
222. Number of nodes of complete binary tree
手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践
科目三: 济南章丘6号线
The fifth strong network cup national network security challenge Title reappearance (with title attachment, detailed explanation)
js实现页面跳转与参数获取加载
C# 使用SqlSugar Updateable系统报错无效数字,如何解决?求指导!
Day 27 of leetcode
开机启动流程及营救模式
Skywalking distributed system application performance monitoring tool - medium
VR全景现在是不是刚需?看完你就明白了
小于等于K的最大子数组累加和
H. 265 web player easyplayer's method of opening video to the public
Subject 3: Jinan Zhangqiu line 3
H.265网页播放器EasyPlayer对外开放录像的方法
STM32CubeMX学习笔记(41)——ETH接口+LwIP协议栈使用(DHCP)