/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
ListNode* slowNode = pListHead;
while (k != 0) { //这里判断 k 一直走到 0 即可
k--;
if (pListHead != nullptr) pListHead = pListHead->next; //在其中判断是否出现k 大于链表总长度的情况,
//比如 【1,2,3,4,5】 6这样的情况,如果出现这样的情况,返回即可
else
return nullptr;
}
while (pListHead != nullptr) { //先走的不能为空
slowNode = slowNode->next;
pListHead = pListHead->next;
}
return slowNode;
}
};