/****************************************************************
* name;find last k
* function:找到单链表中的倒数第k个结点并输出
* parameter;
* @int k
* @head
* ReValue;bool
* author;小北blog
* attention;
* date;2024.04.23
* history;
* version;
* Copyright(c) 2024 huahuadebaby99@163.com All rights Reserved
*****************************************************************/
typedef int Tpyedata_t;
// 创建一个结构体变量
typedef struct LList
{
Tpyedata_t data;
struct LList *next = NULL;
} LL;
// 把k值传入到该链表中,再把创建的新链表传入这函数当中,假设该单向链表已定义成
bool LList *find_last_k(int k, head) // 已经省略错误处理
{
// 定义两个指针变量,和记录循环次数,备份head头指针的地址
int cnt = 0;
LList *L = NULL;
// 遍历单向链表,记录cnt的值
while (L->next)
{
L = L->next;
cnt++;
}
// 做判断,如果k值比cnt大,退出
if (cnt > k)
{
return fales;
}
// 如何做循环,循环(cnt-k)次就是倒数第k个值
while ((cnt - k)--)
{
L->next = L;
}
printf("%d", L->data);
return ture;
}