当前位置:网站首页>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;
}
};
边栏推荐
- 安全运营四要素之资产、脆弱性、威胁和事件
- [applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions
- 1.12 - Instructions
- University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
- Array and collection performance comparison
- FPGA - 7系列 FPGA内部结构之Clocking -04- 多区域时钟
- Hdu3507 (slope DP entry)
- 【日常训练】871. 最低加油次数
- 【AutoSAR 二 AppL概述】
- 【案例分享】让新时代教育发展与“数”俱进
猜你喜欢

2022中国3D视觉企业(引导定位、分拣场景)厂商名单

File operation io-part2

文件操作IO-Part2
![[case sharing] let the development of education in the new era advance with](/img/11/af88d16dc66f00840cbfc5ba5d68bd.jpg)
[case sharing] let the development of education in the new era advance with "number"

Illustrated network: what is virtual router redundancy protocol VRRP?

Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud

【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议

【C语言】分支和循环语句(上)
![[AUTOSAR VI description document]](/img/3d/1382acbc4054ab218485a12b7b4e6b.png)
[AUTOSAR VI description document]

Vulkan-性能及精细化
随机推荐
lex && yacc && bison && flex 配置的問題
tail -f 、tail -F、tailf的区别
1038 Recover the Smallest Number
Teach you JDBC hand in hand -- structure separation
[daily training] 871 Minimum refueling times
Leetcode-224: basic calculator
Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
Vulkan practice first bullet
【JetCache】JetCache的配置说明和注解属性说明
Foundations of data science is free to download
详解RDD基本概念、RDD五大属性
Vulkan-性能及精细化
【AutoSAR 八 OS】
[shutter] image component (configure local GIF image resources | load placeholder with local resources)
Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
Thread start and priority
FPGA - 7系列 FPGA内部结构之Clocking -04- 多区域时钟
kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
2022 list of manufacturers of Chinese 3D vision enterprises (guided positioning and sorting scenes)
[overview of AUTOSAR three RTE]