// 查找链表的倒数第K个结点ide
示意图:spa
PSListNode FindLastKNode(PSListNode pHead, int K )blog
{get
PSListNode pFast = pHead ;it
PSListNode pSlow = pHead ;io
if (pHead == NULL || K <= 0)ast
{class
return NULL ;List
}im
while (--K )
{
if (pFast == NULL )
{
return NULL ;
}
pFast = pFast->pNext;
}
while (pFast->pNext)
{
pSlow = pSlow->pNext;
pFast = pFast->pNext;
}
return pSlow;
}
建议:若是理解不清楚,必定要画个图,看着图写代码会容易不少,思路也会清晰