阅读背景:

查找单链表的倒数第k个结点,要求只能遍历一次链表

来源:互联网 
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;
}pNode FindLastKNode(pList plist, int k)
{



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: