当前位置:网站首页>JZ22 鏈錶中倒數最後k個結點
JZ22 鏈錶中倒數最後k個結點
2022-04-23 02:41: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://yzsam.com/2022/04/202204230240052827.html
边栏推荐
- 解决win7 中powershell挖矿占用CPU100%
- Solve the problem that the registered Google email Gmail mobile number cannot be used for verification
- 双亲委派模型【理解】
- 认识进程(多线程_初阶)
- Global, exclusive and local routing guard
- [XJTU計算機網絡安全與管理]第二講 密碼技術
- The usage and difference of * and & in C language and the meaning of keywords static and volatile
- Huashu "deep learning" and code implementation: 01 Linear Algebra: basic concepts + code implementation basic operations
- Modify the content of MySQL + PHP drop-down box
- Lighting LED of IAR embedded development stm32f103c8t6
猜你喜欢

双亲委派模型【理解】

How to solve the complexity of project document management?

006_ redis_ Jedis quick start
Rich intelligent auxiliary functions and exposure of Sihao X6 security configuration: it will be pre sold on April 23

Day 3 of learning rhcsa
![[untitled]](/img/60/421cda552055664357af47d1a956af.png)
[untitled]

Interpretation of the future development of smart agriculture

Preliminary understanding of stack and queue

How big the program development of single chip microcomputer project can be, it represents your level of knocking code

C语言 171. 最近回文数
随机推荐
[wechat applet] set the bottom menu (tabbar) for the applet
使用Go语言构建Web服务器
Use of go language web Middleware
类初始化和实例初始化面试题
Log4j知识点记录
Data warehouse construction table 111111
Day 3 of learning rhcsa
001_ Redis set survival time
SQL server2019无法下载所需文件,这可能表示安装程序的版本不再受支持,怎么办了
基于Torchserve部署SBERT模型<语义相似度任务>
IAR嵌入式开发STM32f103c8t6之点亮LED灯
TypeScript(1)
Usage of vector common interface
JZ22 链表中倒数最后k个结点
Web learning record (medium)
在MySQL Workbench中执行外部的SQL脚本,报错
Six very 6 computer driver managers: what software is good for driver upgrade? Recommended by the best computer driver management software abroad
Parental delegation model [understanding]
Fashion MNIST dataset classification training
认识进程(多线程_初阶)