当前位置:网站首页>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;
}
};
边栏推荐
- First hand evaluation of Reza electronics rz/g2l development board
- [C language] branch and loop statements (Part 1)
- Leetcode-849: maximum distance to the nearest person
- 研发一款国产ARM智能边缘计算网关需要什么
- Linear programming of mathematical modeling (including Matlab code)
- Infrared thermography temperature detection system based on arm rk3568
- matlab将数字矩阵保存为地理空间数据出错,显示下标索引必须为正整数类型或逻辑类型,解决
- Usage of using clause in kingbases alter table
- In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
- 【AutoSAR 七 工具链简介】
猜你喜欢
随机推荐
Explain the basic concepts and five attributes of RDD in detail
Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
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 performance and refinement
leetcode-241:为运算表达式设计优先级
Illustrated network: what is virtual router redundancy protocol VRRP?
解决ReactNative使用webView存在缓存问题
机器学习:numpy版本线性回归预测波士顿房价
[overview of AUTOSAR three RTE]
这不平凡的两年,感谢我们一直在一起!
tail -f 、tail -F、tailf的区别
详解RDD基本概念、RDD五大属性
Overlay of shutter (Pop-Up)
FPGA - 7系列 FPGA内部结构之Clocking -04- 多区域时钟
合并K个已排序的链表
Deep analysis of data storage in memory
1.12 - Instructions


![[AUTOSAR I overview]](/img/e4/b97c6beebf6f431d2d7cf209c6683e.png)






