当前位置:网站首页>Inversion de l'intervalle spécifié dans la liste des liens
Inversion de l'intervalle spécifié dans la liste des liens
2022-07-03 01:01:00 【Schuyler Hu】
Questions
Nombre de noeuds size Liste des liens m Position n Inversion de section entre les positions,Complexité temporelle requise O(n),Complexité spatiale O(1).
Idées
- Déplacer à la position spécifiée avec un double pointeur: pre Passer à la position précédente de la position de départ de la Section de déplacement,cur Déplacer vers la position de départ de la Section de déplacement.
- Connexion cur Avec cur L'élément suivant de,Déconnecter cur Avec next Connexion à;next Connectez - vous à pre Avant ce dernier élément;pre Pointage next.
Mise en œuvre du Code
/** * struct ListNode { * int val; * struct ListNode *next; * }; */
class Solution {
public:
/** * * @param head ListNodeCatégorie * @param m intTaille * @param n intTaille * @return ListNodeCatégorie */
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, Donc l'indice de cette liste de questions vient de 1 C'est parti.,pre Pointe vers la position avant le début de l'intervalle de retournement
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;
// Déconnecter cur Avec next Connexion à
cur->next = next->next;
// À suivre next Déplacer vers pre Avant l'élément suivant
next->next = pre->next;
// Connexion pre Et next
pre->next = next;
}
return dummyHead->next;
}
};
边栏推荐
- Extension of flutter
- [AUTOSAR nine c/s principle Architecture]
- 链表内指定区间反转
- 解决ReactNative使用webView存在缓存问题
- 【AutoSAR 四 BSW概述】
- Reading and writing speed of Reza rz/g2l arm development board storage and network measurement
- Leetcode-2115: find all the dishes that can be made from the given raw materials
- [case sharing] let the development of education in the new era advance with "number"
- 1.12 - 指令
- 数据分析思维分析犯法和业务知识——分析方法(一)
猜你喜欢
University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
2022上半年值得被看见的10条文案,每一句都能带给你力量!
Sentry developer contribution Guide - configure pycharm
【AutoSAR 九 C/S原理架构】
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
指针初阶(基础)
测试右移:线上质量监控 ELK 实战
[AUTOSAR eight OS]
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight
随机推荐
Initial order of pointer (basic)
Callback event after the antv X6 node is dragged onto the canvas (stepping on a big hole record)
【AutoSAR 五 方法论】
机器学习:numpy版本线性回归预测波士顿房价
2022上半年值得被看见的10条文案,每一句都能带给你力量!
[daily training] 871 Minimum refueling times
【JetCache】JetCache的配置说明和注解属性说明
Arduino开发之按键检测与正弦信号输出
[Luogu p4320] road meets (round square tree)
The difference between relational database and non relational database
Sentry developer contribution Guide - configure pycharm
2022.2.14 resumption
Vulkan is not a "panacea"“
Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud
深度剖析数据在内存中的存储
1.11 - 总线
[love crash] neglected details of gibaro
[jetcache] jetcache configuration description and annotation attribute description
递归处理组织的几种情况
Leetcode-2115: find all the dishes that can be made from the given raw materials