当前位置:网站首页>Les derniers noeuds K de la liste jz22
Les derniers noeuds K de la liste jz22
2022-04-23 02:41:00 【Rosita.】
Liens vers les sujets:L'avant - dernier de la ListekNoeuds_Le problème de la vache_Filet de taureaux
Attention:
1.Recherche de violence:Trouvez d'abord la longueur de la listelen,Si moins dek,Retourne à NULL,Pas moins dek,Allons - y.len-k- Non.,C'est ce qui s'est passé.kEmplacement et éléments suivants
2.Pointeur rapide et lent:Pointeur lent vers le noeud de tête,Le pointeur rapide passe en premierkPas,Plus long que la longueur de la liste de liens renvoie NULL,Si le pointeur rapide arrive à la fin,Le pointeur lent pointe verskNoeuds.
Table des matières
Méthode 1:Recherche de violence
Méthode 2:Pointeur rapide et lent
Méthode 1:Recherche de violence
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* Nom de la classe dans le Code、Nom de la méthode、Nom du paramètre spécifié,Ne pas modifier,Renvoie directement la valeur spécifiée par la méthode
*
*
* @param pHead ListNodeCatégorie
* @param k intTaille
* @return ListNodeCatégorie
*/
ListNode* FindKthToTail(ListNode* pHead, int k) {
int len = 0;
ListNode *p = pHead;
while(p){
len++;
p = p->next;
}
if(len < k) return NULL;
p = pHead;
for(int i = 0 ; i < len-k; ++i){
p = p->next;
}
return p;
}
};
Méthode 2:Pointeur rapide et lent
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* Nom de la classe dans le Code、Nom de la méthode、Nom du paramètre spécifié,Ne pas modifier,Renvoie directement la valeur spécifiée par la méthode
*
*
* @param pHead ListNodeCatégorie
* @param k intTaille
* @return ListNodeCatégorie
*/
ListNode* FindKthToTail(ListNode* pHead, int k) {
ListNode* fast = pHead ,*slow = pHead;
//Le pointeur rapide passe en premierkPas
for(int i = 0; i < k; ++i){
if(fast != nullptr){
fast = fast->next;
}else{
return slow = nullptr;
}
}
while(fast){
fast = fast->next;
slow = slow->next;
}
return slow;
}
};
版权声明
本文为[Rosita.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230240052827.html
边栏推荐
- Efficient music format conversion tool Music Converter Pro
- RT_Thread自问自答
- The second day of learning rhcsa
- 高效音乐格式转换工具Music Converter Pro
- SQL server2019 cannot download the required files, which may indicate that the version of the installer is no longer supported. What should I do
- 16、 Anomaly detection
- 5W of knowledge points
- Lighting LED of IAR embedded development stm32f103c8t6
- JDBC JDBC
- 一、序列模型-sequence model
猜你喜欢
The 16th day of sprint to the big factory, noip popularization Group Three Kingdoms game
RT_Thread自问自答
Understanding process (multithreading primary)
Six very 6 computer driver managers: what software is good for driver upgrade? Recommended by the best computer driver management software abroad
打靶narak
智能辅助功能丰富,思皓X6安全配置曝光:将于4月23日预售
Those years can not do math problems, using pyhon only takes 1 minute?
机器学习(周志华) 第十四章概率图模型
Huashu "deep learning" and code implementation: 01 Linear Algebra: basic concepts + code implementation basic operations
Target narak
随机推荐
Fashion MNIST 数据集分类训练
Day18 -- stack queue
win查看端口占用 命令行
RT_ Thread ask and answer
【无标题】
Suggestion: block reference sorting is in the order of keywords
重大危险源企业如何保障年底前完成双预防机制数字化建设任务
[xjtu Computer Network Security and Management] session 2 Cryptographic Technology
RT_Thread自问自答
006_ redis_ Sortedset type
Servlet template engine usage example
JDBC JDBC
Global, exclusive, local Routing Guard
Implementation of distributed scenario business operation log (based on redis lightweight)
The second day of learning rhcsa
全局、獨享、局部路由守衛
Use of go language web Middleware
IAR embedded development stm32f103c8t6 Lighting LED
想用Mac学习sql,主要给自己个充足理由买Mac听听意见
009_ Redis_ Getting started with redistemplate