当前位置:网站首页>Specified interval inversion in the linked list
Specified interval inversion in the linked list
2022-07-03 01:01:00 【Schuyler Hu】
problem
Set the number of a node to size Linked list m Position to n Interval reversal between positions , Time complexity required O(n), Spatial complexity O(1).
Ideas
- Use double pointer to move to the specified position : pre Move to the previous position of the starting position of the mobile section ,cur Move to the starting position of the mobile section .
- Connect cur And cur Next element of , To break off cur And next The connection of ;next Connect to pre Before the next element ;pre Point to next.
Code implementation
/** * struct ListNode { * int val; * struct ListNode *next; * }; */
class Solution {
public:
/** * * @param head ListNode class * @param m int integer * @param n int integer * @return ListNode class */
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, So the subscript of the linked list of this question is from 1 Start ,pre Point to the position before the beginning of the overturning section
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;
// To break off cur And next The connection of
cur->next = next->next;
// The following next Move to pre Before the next element
next->next = pre->next;
// Connect pre and next
pre->next = next;
}
return dummyHead->next;
}
};
边栏推荐
- [shutter] image component (cached_network_image network image caching plug-in)
- Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
- Is there a free text to speech tool to help recommend?
- First hand evaluation of Reza electronics rz/g2l development board
- Tensorflow 2.x(keras)源码详解之第十五章:迁移学习与微调
- [case sharing] let the development of education in the new era advance with "number"
- [AUTOSAR + IO Architecture]
- Leetcode-2115: find all the dishes that can be made from the given raw materials
- Vulkan-性能及精细化
- 数据分析思维分析犯法和业务知识——分析方法(一)
猜你喜欢

安全运营四要素之资产、脆弱性、威胁和事件

(C语言)数据的存储

详解RDD基本概念、RDD五大属性

Rk3568 development board evaluation (II): development environment construction

【案例分享】让新时代教育发展与“数”俱进

瑞萨电子RZ/G2L开发板上手评测

Vulkan practice first bullet
![[AUTOSAR nine c/s principle Architecture]](/img/59/ce32c0ff58ef5d8385fe950136175b.png)
[AUTOSAR nine c/s principle Architecture]

拥抱平台化交付的安全理念
![[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)](/img/f5/3ec22f1480227f33a1c8ac457155ed.jpg)
[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)
随机推荐
【AutoSAR 十 IO架构】
tail -f 、tail -F、tailf的区别
1.11 - 总线
瑞萨电子RZ/G2L开发板上手评测
How to systematically learn machine learning
Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
Vulkan并非“灵药“
2022.2.14 resumption
Tensorflow 2.x(keras)源码详解之第十五章:迁移学习与微调
In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
Arduino开发之按键检测与正弦信号输出
2022中国3D视觉企业(引导定位、分拣场景)厂商名单
合并K个已排序的链表
数学建模之线性规划(含MATLAB代码)
基于ARM RK3568的红外热成像体温检测系统
465. DFS backtracking of optimal bill balance
Several cases of recursive processing organization
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
matlab将数字矩阵保存为地理空间数据出错,显示下标索引必须为正整数类型或逻辑类型,解决
Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)