pNode FindLastKNode(pList plist, int k)
{
pNode pFast = plist;
pNode pSlow = plist;
if (plist == NULL || k <= 0)
{
return NULL;
}
while (k--)//pFast先走k步
{
if (pFast == NULL)//k大于链表中结点的个数
{
return NULL;
}
pFast = pFast->next;
}
while (pFast)//两个指针同时朝后走
{
pFast = pFast->next;
pSlow = pSlow->next;
}
return pSlow;
}
查找单链表的倒数第k个结点,要求只能遍历一次链表
猜你喜欢
转载自blog.csdn.net/weixin_40995778/article/details/82825080
今日推荐
周排行