题解 | #链表中倒数最后k个结点#
链表中倒数最后k个结点
https://www.nowcoder.com/practice/886370fe658f41b498d40fb34ae76ff9
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : val(x), next(nullptr) {} * }; */ class Solution { public: ListNode* FindKthToTail(ListNode* pHead, int k) { int count=0; ListNode* p=pHead; while(p!=nullptr) { count++; p=p->next; } if(count<k) return nullptr; ListNode* low=pHead; ListNode* fast=pHead; for(int i=0;i<k;++i) { fast=fast->next; } while(fast!=nullptr) { fast=fast->next; low=low->next; } return low; } };