这里主要使考虑代码的强壮性。
ListNode* ListLastK(ListNode* pHead, int K)
{
if (NULL == pHead || K == 0)
return NULL;
ListNode* pFast = pHead;
for (unsigned int i = 0; i < K - 1; ++1)
{
if (pFast->pNext != NULL)
{
pFast = pFast->pNext;
}
else
{
return NULL;
}
}
ListNode* pSlow = pHead;
while (pSlow != NULL)
{
pSlow = pSlow->pNext;
pFast = pFast->pNext;
}
return pSlow;
}