// 查找链表的倒数第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;
}
建议:如果理解不清楚,一定要画个图,看着图写代码会容易很多,思路也会清晰
时间: 2024-10-26 13:31:57