当前位置:网站首页>链表内指定区间反转
链表内指定区间反转
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;
}
};
边栏推荐
- File operation io-part2
- Cordova plugin device obtains the device information plug-in, which causes Huawei to fail the audit
- RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
- 瑞萨电子RZ/G2L开发板上手评测
- 数学建模之线性规划(含MATLAB代码)
- Vulkan并非“灵药“
- Shell implements basic file operations (cutting, sorting, and de duplication)
- [AUTOSAR VI description document]
- [introduction to AUTOSAR seven tool chain]
- 研发一款国产ARM智能边缘计算网关需要什么
猜你喜欢
![[AUTOSAR II appl overview]](/img/da/76ccc05e2199705b20d8304bfb86b2.png)
[AUTOSAR II appl overview]

Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
![[AUTOSAR nine c/s principle Architecture]](/img/59/ce32c0ff58ef5d8385fe950136175b.png)
[AUTOSAR nine c/s principle Architecture]

全志A40i/T3如何通过SPI转CAN
![[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions](/img/ea/ee1ad50a497478b9d080bb5e4bdfb5.png)
[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions

Liad: the consumer end of micro LED products is first targeted at TVs above 100 inches. At this stage, it is still difficult to enter a smaller size

测试右移:线上质量监控 ELK 实战

Rust string slicing, structs, and enumeration classes

First hand evaluation of Reza electronics rz/g2l development board

【AutoSAR 十 IO架构】
随机推荐
1.12 - Instructions
Nacos+openfeign error reporting solution
antv x6节点拖拽到画布上后的回调事件(踩大坑记录)
Reading and writing speed of Reza rz/g2l arm development board storage and network measurement
About qbytearray storage hexadecimal and hexadecimal conversion
File operation io-part2
Cordova plugin device obtains the device information plug-in, which causes Huawei to fail the audit
matlab将数字矩阵保存为地理空间数据出错,显示下标索引必须为正整数类型或逻辑类型,解决
leetcode-1964:找出到每个位置为止最长的有效障碍赛跑路线
世平信息首席科学家吕喆:构建以数据和人员为中心的安全能力
First hand evaluation of Reza electronics rz/g2l development board
Array common operation methods sorting (including ES6) and detailed use
文件操作IO-Part2
Leetcode-241: designing priorities for operational expressions
[case sharing] let the development of education in the new era advance with "number"
Solve the cache problem of reactnative using WebView
[Luogu p4320] road meets (round square tree)
【AutoSAR 二 AppL概述】
kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
【AutoSAR 九 C/S原理架构】