当前位置:网站首页>剑指 Offer 22. 链表中倒数第k个节点
剑指 Offer 22. 链表中倒数第k个节点
2022-07-02 14:21:00 【anieoo】
solution:
快慢指针,快指针先走k步,然后快慢指针一起向前走,当快指针走到终点的时候,慢指针就是倒数第k个。
class Solution {
public:
ListNode* getKthFromEnd(ListNode* head, int k) {
ListNode *fast = head;
ListNode *slow = head;
while(fast != NULL && k) {
fast = fast->next;
k--;
}
while(fast != NULL) {
fast = fast->next;
slow = slow->next;
}
return slow;
}
};
边栏推荐
- jsp 和 servlet 有什么区别?
- 远程办公对我们的各方面影响心得 | 社区征文
- Easy language ABCD sort
- 【Leetcode】13. Roman numeral to integer
- 对接保时捷及3PL EDI案例
- 【Leetcode】14. 最长公共前缀
- OpenPose的使用
- Executive engine module of high performance data warehouse practice based on Impala
- Detailed explanation of @accessories annotation of Lombok plug-in
- Interpretation of key parameters in MOSFET device manual
猜你喜欢
pwm呼吸灯
⌈ 2022 ⌋ how to use webp gracefully in projects
Tech Talk 活动预告 | 基于Amazon KVS打造智能视觉产品
Cell:清华程功组揭示皮肤菌群的一种气味挥发物促进黄病毒感染宿主吸引蚊虫...
如何与博格华纳BorgWarner通过EDI传输业务数据?
基于Impala的高性能数仓实践之执行引擎模块
Kubernetes three open interfaces first sight
Tech talk activity preview | building intelligent visual products based on Amazon kVs
TCP server communication process (important)
只是巧合?苹果iOS16的神秘技术竟然与中国企业5年前产品一致!
随机推荐
Youzan won the "top 50 Chinese enterprise cloud technology service providers" together with Tencent cloud and Alibaba cloud [easy to understand]
The impact of telecommuting on all aspects of our experience | community essay solicitation
基于多元时间序列对高考预测分析案例
社交元宇宙平台Soul冲刺港股:年营收12.8亿 腾讯是股东
Lampe respiratoire PWM
如何与博格华纳BorgWarner通过EDI传输业务数据?
AP and F107 data sources and processing
System Verilog实现优先级仲裁器
LSF basic command
福元医药上交所上市:市值105亿 胡柏藩身价超40亿
[error record] error -32000 received from application: there are no running service protocol
Seven charts, learn to do valuable business analysis
Day 18 of leetcode dynamic planning introduction
一文看懂:数据指标体系的4大类型
Talk about an experience of job hopping and being rejected
L'explosion de John utilise l'encodage d'entrée par défaut: UTF - 8 Loaded 1 password Hash (bcrypt [blowfish 32 / 64 X3])
Easy language ABCD sort
【Leetcode】13. 罗马数字转整数
机器学习-感知机模型
Kubernetes three open interfaces first sight