当前位置:网站首页>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;
}
};
边栏推荐
- 【AutoSAR 四 BSW概述】
- 【AutoSAR 五 方法论】
- 12_微信小程序之微信视频号滚动自动播放视频效果实现
- [AUTOSAR I overview]
- Leetcode-241: designing priorities for operational expressions
- [shutter] image component (configure local GIF image resources | load placeholder with local resources)
- 【案例分享】让新时代教育发展与“数”俱进
- Is there a free text to speech tool to help recommend?
- Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud
- 安全运营四要素之资产、脆弱性、威胁和事件
猜你喜欢
详解RDD基本概念、RDD五大属性
2022 list of manufacturers of Chinese 3D vision enterprises (guided positioning and sorting scenes)
2022中国3D视觉企业(引导定位、分拣场景)厂商名单
Hdu3507 (slope DP entry)
Vulkan-性能及精细化
[AUTOSAR II appl overview]
Deep analysis of data storage in memory
【AutoSAR 八 OS】
【AutoSAR 四 BSW概述】
12_微信小程序之微信视频号滚动自动播放视频效果实现
随机推荐
世平信息首席科学家吕喆:构建以数据和人员为中心的安全能力
Rust ownership (very important)
465. 最优账单平衡 DFS 回溯
leetcode-2280:表示一个折线图的最少线段数
【C语言】分支和循环语句(上)
数据分析思维分析犯法和业务知识——分析方法(一)
瑞萨电子RZ/G2L开发板上手评测
[AUTOSAR 11 communication related mechanism]
Vulkan-性能及精细化
【AutoSAR 四 BSW概述】
Rust string slicing, structs, and enumeration classes
leetcode-1964:找出到每个位置为止最长的有效障碍赛跑路线
[overview of AUTOSAR four BSW]
excel去除小数点后面的数据,将数字取整
RK3568开发板评测篇(二):开发环境搭建
【AutoSAR 三 RTE概述】
In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
Leetcode-871: minimum refueling times
Vulkan is not a "panacea"“
leetcode-871:最低加油次数