当前位置:网站首页>链表内指定区间反转
链表内指定区间反转
2022-07-03 00:29:00 【Schuyler Hu】
问题
将一个节点数为 size 链表 m 位置到 n 位置之间的区间反转,要求时间复杂度 O(n),空间复杂度 O(1)。
思路
- 采用双指针移动到指定位置: pre 移动到挪动区间的起始位置的前一个位置,cur 移动到挪动区间的起始位置。
- 连接 cur 与 cur 的下下个元素,断开 cur 与 next 的连接;next 连接到 pre 后一元素之前;pre 指向 next。
代码实现
/** * 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) {
// write code here
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode* pre = dummyHead;
// 0 < m < size,所以本题链表下标从 1 开始,pre 指向翻转区间的起始前一个位置
for (int i = 1; i < m; i++)
{
pre = pre->next;
}
ListNode* cur = pre->next;
for (int i = m; i < n; i++)
{
ListNode* next = cur->next;
// 断开 cur 与 next 的连接
cur->next = next->next;
// 将后续的 next 移动到 pre 下一个元素前
next->next = pre->next;
// 连接 pre 和 next
pre->next = next;
}
return dummyHead->next;
}
};
边栏推荐
- Leetcode-934: the shortest Bridge
- 1.12 - 指令
- Leetcode-2280: represents the minimum number of line segments of a line graph
- 基于ARM RK3568的红外热成像体温检测系统
- 图解网络:什么是虚拟路由器冗余协议 VRRP?
- 机器学习:numpy版本线性回归预测波士顿房价
- Thank you for being together for these extraordinary two years!
- Rust string slicing, structs, and enumeration classes
- Is there a free text to speech tool to help recommend?
- [shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)
猜你喜欢
![[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)](/img/73/19e2e0fc5ea6f05e34584ba40a452d.jpg)
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)

Deep analysis of data storage in memory

How SQLSEVER removes data with duplicate IDS
![[case sharing] let the development of education in the new era advance with](/img/11/af88d16dc66f00840cbfc5ba5d68bd.jpg)
[case sharing] let the development of education in the new era advance with "number"

这不平凡的两年,感谢我们一直在一起!

Foundations of data science is free to download

【AutoSAR 一 概述】

Detailed explanation of pod life cycle
![[overview of AUTOSAR four BSW]](/img/19/c2273bbedb7f8d859e5a3805ed5740.png)
[overview of AUTOSAR four BSW]

图解网络:什么是虚拟路由器冗余协议 VRRP?
随机推荐
Overlay of shutter (Pop-Up)
An excellent orm in dotnet circle -- FreeSQL
Detailed explanation of pod life cycle
[AUTOSAR 11 communication related mechanism]
关于QByteArray存储十六进制 与十六进制互转
Set up nacos2 X cluster steps and problems encountered
数据分析思维分析犯法和业务知识——分析方法(一)
Win10 多种方式解决无法安装.Net3.5的问题
【爱死机】《吉巴罗》被忽略的细节
Leetcode-224: basic calculator
University of Oslo: Li Meng | deep reinforcement learning based on swing transformer
瑞萨RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南
AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight
Key detection and sinusoidal signal output developed by Arduino
File operation io-part2
Vulkan is not a "panacea"“
Shell implements basic file operations (cutting, sorting, and de duplication)
[AUTOSAR twelve mode management]
leetcode-849:到最近的人的最大距离
【AutoSAR 三 RTE概述】