单链表的实现https://blog.csdn.net/weixin_41892460/article/details/82855823

1.判断单链表是否带环

pNode IsCrossH(pList list1)
{
	pNode fast = list1;
	pNode slow = list1;
	if (NULL == list1)
		return NULL;
	while (fast&&fast->next)
	{
		fast = fast->next->next;
		slow = slow->next;
		if (slow == fast)
			return slow;
	}
	return NULL;
}

2.求环的长度

int GetCircleLength(pNode meet)
{
	pNode cur = meet;
	int len = 1;
	if (NULL == meet)
	{
		return 0;
	}
	else
	{
		while (cur->next != meet)
		{
			cur = cur->next;
			len++;
		}
	}
	return len;
}

3.求环的入口点

【数据结构】判断单链表是否带环若带环求环的长度求环的入口点

pNode GetCycleEntryNode(pList list)
{
	pNode plist = list;
	pNode pmeet = IsCrossH(list);
	if (NULL == list)
		return NULL;
	else
	{
		while (plist != pmeet)
		{
			plist = plist->next;
			pmeet = pmeet->next;
		}
	}
	return pmeet;
}

相关文章: