当前位置:网站首页>【链表中倒数第k个结点】
【链表中倒数第k个结点】
2022-06-22 19:41:00 【爱吃榴莲的喵星人】
一、题目描述
描述
输入一个链表,输出该链表中倒数第k个结点。
示例1
输入:1,{1,2,3,4,5}
返回值:{5}
二、提供方便走读代码的图

三、题目代码
思路:fast先走k步,然后slow与fast再一起走。fast==NULL时,slow就是倒数第k个
/** * struct ListNode { * int val; * struct ListNode *next; * }; * * C语言声明定义全局变量请加上static,防止重复定义 * * C语言声明定义全局变量请加上static,防止重复定义 */
/** * * @param pListHead ListNode类 * @param k int整型 * @return ListNode类 */
struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {
// write code here
struct ListNode* fast=pListHead;
struct ListNode* slow=pListHead;
while(k--)
{
if(fast==NULL)
return NULL;
fast=fast->next;
}
while(fast)
{
fast=fast->next;
slow=slow->next;
}
return slow;
}
以上是本篇文章的全部内容,如果文章有错误或者有看不懂的地方,多和喵博主交流。互相学习互相进步。如果这篇文章对你有帮助,可以给喵博主一个关注,你们的支持是我最大的动力。
边栏推荐
猜你喜欢
![[proteus simulation] 8x8LED dot matrix digital cyclic display](/img/a9/0107eb02c7a081fb466b1c7c12baef.png)
[proteus simulation] 8x8LED dot matrix digital cyclic display

MySQL advanced (II)

Using qtest for data set test performance test GUI test

Introduction of neural networks for Intelligent Computing (Hopfield network DHNN, CHNN)

How to realize @ person function in IM instant messaging

R language usarrests dataset visualization

一行代码为特定状态绑定SwiftUI视图动画

【513. 找树左下角的值】

极客星球 | 业务监控及告警系统体系化建设之路

R语言 co2数据集 可视化
随机推荐
88-被广为流传的参数优化, 是蜜糖还是毒药?
Software testing - Test Case Design & detailed explanation of test classification
80-分页查询,不止写法
How to use feign to construct multi parameter requests
怎样实现网页端im即时通讯中的@人功能
Feign FAQ summary
Golang学习笔记—结构体
扩展Ribbon支持基于元数据的版本管理
NBA季后赛对阵图
【观察】软件行业创新进入“新周期”,如何在变局中开新局?
Using qtest for data set test performance test GUI test
Introduction of neural networks for Intelligent Computing (Hopfield network DHNN, CHNN)
2022团体程序设计天梯赛L1
How to realize @ person function in IM instant messaging
慕课6、实现负载均衡-Ribbon
Performance test (I)
what? You can't be separated by wechat
NumPy学习笔记(六)——sum()函数
Cross domain cors/options
He was in '98. I can't play with him