// 查找链表的倒数第K个结点
示意图:
PSListNode FindLastKNode(PSListNode pHead, int K )
{
PSListNode pFast = pHead ;
PSListNode pSlow = pHead ;
if (pHead == NULL || K <= 0)
{
return NULL ;
}
while (--K )
{
if (pFast == NULL )
{
return NULL ;
}
pFast = pFast->pNext;
}
while (pFast->pNext)
{
pSlow = pSlow->pNext;
pFast = pFast->pNext;
}
return pSlow;
}
建议:如果理解不清楚,一定要画个图,看着图写代码会容易很多,思路也会清晰
转载于:https://blog.51cto.com/10921020/1760721