题目
输入一个链表,输出该链表中倒数第k个结点。
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
if(pListHead == nullptr || k == 0)
return nullptr;
ListNode * pAhead = pListHead;
ListNode * pBahead = nullptr;
for(unsigned int i = 0; i < k - 1; i++)
{
if(pAhead->next != nullptr){
pAhead = pAhead->next;
}
else
{
return nullptr;
}
}
pBahead = pListHead;
while(pAhead->next != nullptr)
{
pAhead = pAhead->next;
pBahead = pBahead->next;
}
return pBahead;
}
};