当前位置:网站首页>The penultimate K nodes in jz22 linked list
The penultimate K nodes in jz22 linked list
2022-04-23 02:41:00 【Rosita.】
Topic link : Last in the list k Nodes _ Niuke Tiba _ Cattle from
Be careful :
1. Violent search : First, find the length of the linked list len, If it is less than k, Return empty , Not less than k, go len-k No , The result is k Elements at and after the first position
2. Speed pointer : The slow pointer points to the head node , Let's go first k Step , If it is longer than the length of the linked list, return null , If the pointer goes to the end , The slow pointer points to k Nodes .
Catalog
Method 1 : Violent search
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly
*
*
* @param pHead ListNode class
* @param k int integer
* @return ListNode class
*/
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;
}
};
Method 2 : Speed pointer
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly
*
*
* @param pHead ListNode class
* @param k int integer
* @return ListNode class
*/
ListNode* FindKthToTail(ListNode* pHead, int k) {
ListNode* fast = pHead ,*slow = pHead;
// Let's go first k Step
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
边栏推荐
猜你喜欢
[unity3d] rolling barrage effect in live broadcasting room
Flink learning (XI) watermark
【2019-CVPR-3D人体姿态估计】Fast and Robust Multi-Person 3D Pose Estimation from Multiple Views
Using go language to build web server
The 16th day of sprint to the big factory, noip popularization Group Three Kingdoms game
手写内存池以及原理代码分析【C语言】
006_ redis_ Jedis quick start
解决win7 中powershell挖矿占用CPU100%
[XJTU computer network security and management] Lecture 2 password technology
php+mysql對下拉框搜索的內容修改
随机推荐
字符串去掉空格问题
[untitled]
每日一题冲刺大厂第十六天 NOIP普及组 三国游戏
16、 Anomaly detection
[wechat applet] set the bottom menu (tabbar) for the applet
Fashion MNIST dataset classification training
1215_ Hello world used by scons
MySQL JDBC programming
Leetcode cooking
Talk about current limiting
[xjtu Computer Network Security and Management] session 2 Cryptographic Technology
5W of knowledge points
Using go language to build web server
The second day of learning rhcsa
Interpretation of the future development of smart agriculture
TypeScript(1)
Parental delegation model [understanding]
SQL server2019 cannot download the required files, which may indicate that the version of the installer is no longer supported. What should I do
IAR嵌入式開發STM32f103c8t6之點亮LED燈
JZ35 复杂链表的复制