当前位置:网站首页>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;
}
};
边栏推荐
- Test shift right: Elk practice of online quality monitoring
- Win10 can't be installed in many ways Problems with NET3.5
- Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
- Leetcode-871: minimum refueling times
- 【案例分享】让新时代教育发展与“数”俱进
- Solve the cache problem of reactnative using WebView
- [flutter] icons component (load the built-in icon of flutter | display the material design icon completely)
- RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
- 数据分析思维分析犯法和业务知识——分析方法(一)
- 【AutoSAR 四 BSW概述】
猜你喜欢

How to systematically learn machine learning

这不平凡的两年,感谢我们一直在一起!

1.11 - bus

The difference between tail -f, tail -f and tail

文件操作IO-Part2

Advanced pointer (I)

Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities
![[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"
![[introduction to AUTOSAR seven tool chain]](/img/cf/ed0ccf39d38e0b4fc3d97d4fd58a7e.png)
[introduction to AUTOSAR seven tool chain]

图解网络:什么是虚拟路由器冗余协议 VRRP?
随机推荐
【JetCache】JetCache的配置说明和注解属性说明
1.11 - 总线
详解RDD基本概念、RDD五大属性
Set up nacos2 X cluster steps and problems encountered
Thread start and priority
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
Shell implements basic file operations (cutting, sorting, and de duplication)
1.12 - 指令
这不平凡的两年,感谢我们一直在一起!
How to convert Quanzhi a40i/t3 to can through SPI
leetcode-2280:表示一个折线图的最少线段数
Machine learning: numpy version linear regression predicts Boston house prices
Extension of flutter
University of Oslo: Li Meng | deep reinforcement learning based on swing transformer
Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud
[AUTOSAR five methodology]
研发一款国产ARM智能边缘计算网关需要什么
【AutoSAR 十 IO架构】
Hdu3507 (slope DP entry)
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3