当前位置:网站首页>JZ22 链表中倒数最后k个结点
JZ22 链表中倒数最后k个结点
2022-04-23 02:40:00 【Rosita.】
题目链接:链表中倒数最后k个结点_牛客题霸_牛客网
注意点:
1.暴力查找:首先求链表长度len,如果小于k,就返回空,不小于k,走len-k不,得出的就是k个位置及后的元素
2.快慢指针:慢指针指向头节点,快指针先走k步,大于链表长度返回空,如果快指针走到尾,慢指针指向的就是k个节点。
目录
方法一:暴力查找
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pHead ListNode类
* @param k int整型
* @return ListNode类
*/
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;
}
};
方法二:快慢指针
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pHead ListNode类
* @param k int整型
* @return ListNode类
*/
ListNode* FindKthToTail(ListNode* pHead, int k) {
ListNode* fast = pHead ,*slow = pHead;
//快指针先走k步
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://blog.csdn.net/pure_dreams/article/details/124342917
边栏推荐
- The second day of learning rhcsa
- hack the box optimum靶机
- Rhcsa day 4 operation
- Usage of vector common interface
- 全局、獨享、局部路由守衛
- Leetcode cooking
- 基于Torchserve部署SBERT模型<语义相似度任务>
- 十六、异常检测
- Niuke hand speed monthly race 48 C (I can't understand the difference. It belongs to yes)
- The usage and difference of * and & in C language and the meaning of keywords static and volatile
猜你喜欢
Fashion MNIST dataset classification training
Halo open source project learning (I): project launch
电源电路设计原来是这么回事
[XJTU computer network security and management] Lecture 2 password technology
魔王冷饭||#078 魔王答上海、南京行情;沟通指导;得国和打杀筛选;赚钱的目的;改变别人看法
006_ redis_ Jedis quick start
【2019-CVPR-3D人体姿态估计】Fast and Robust Multi-Person 3D Pose Estimation from Multiple Views
接口请求时间太长,jstack观察锁持有情况
How to prevent leakage of operation and maintenance data
MySQL JDBC编程
随机推荐
PIP install shutil reports an error
[xjtu Computer Network Security and Management] session 2 Cryptographic Technology
全局、獨享、局部路由守衛
Efficient music format conversion tool Music Converter Pro
JDBC JDBC
C # import details
Handwritten memory pool and principle code analysis [C language]
Preliminary understanding of stack and queue
How to solve the complexity of project document management?
高效音乐格式转换工具Music Converter Pro
Flink stream processing engine system learning (II)
Those years can not do math problems, using pyhon only takes 1 minute?
1、 Sequence model
定了,今日起,本号粉丝可免费参与网易数据分析培训营!
使用Go语言构建Web服务器
Water diversion into chengluo Valley p1514
Push data from onenet cloud platform to database
Develop a chrome plug-in from 0 (2)
002_ Redis_ Common operation commands of string type
Latin goat (20204-2022) - daily question 1