😈博客主页:🐼大家好我叫张同学🐼
💖 欢迎点赞 👍 收藏 💗留言 📝 欢迎讨论! 👀
🎵本文由 【大家好我叫张同学】 原创,首发于 CSDN 🌟🌟🌟
✨精品专栏(不定时更新) 【数据结构+算法】 【做题笔记】【C语言编程学习】
☀️ 精品文章推荐
【C语言进阶学习笔记】三、字符串函数详解(1)(爆肝吐血整理,建议收藏!!!)
【C语言基础学习笔记】+【C语言进阶学习笔记】总结篇(坚持才有收获!)

原题链接(点击跳转)

struct ListNode* getKthFromEnd(struct ListNode* head, int k){
int length = 0;
struct ListNode* cur = head;
while(cur){
length++;
cur = cur->next;
}
cur = head;
int step = length-k;
while(step--){
cur = cur->next;
}
return cur;
}

if(k > length)
return NULL;

struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {
int length = 0;
int step = 0;
struct ListNode* cur = pListHead;
while(cur){
length++;
cur = cur->next;
}
if(k > length)
return NULL;
cur = pListHead;
step = length-k;
while(step--){
cur = cur->next;
}
return cur;
}


struct ListNode* getKthFromEnd(struct ListNode* head, int k){
struct ListNode *fast = head,*slow = head;
while(k--){
fast = fast->next;
}
while(fast){
fast = fast->next;
slow = slow->next;
}
return slow;
}

struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {
struct ListNode *fast = pListHead,*slow = pListHead;
if(k == 0)
return NULL;
while(k--){
if(fast == NULL)
return NULL;
fast = fast->next;
}
while(fast){
fast = fast->next;
slow = slow->next;
}
return slow;
}
