单链表是否带环?
用快慢指针分别遍历这个单链表,如果快慢指针能相遇,就代表有环的存在。
环的长度?
当快慢指针相遇时,记录当前位置,然后走一圈的长度就是环的长度。
环的入口点?
设快指针F走两个结点,慢指针S走一个结点。
F=2*S; //快指针是慢指针走的两倍
S=L1+a; //慢指针相遇时走的长度
F=L1+n*L2+a; //快指针相遇时走的长度
通过前三个式子得:n*L2=L1+a 即 L1=(n-1)L2+(L2-a)
环外的链表长度=环长度的整数倍+(环长度-快慢指针相遇点到入口点的距离)
int HasListCircle(pList* pHead)//有环?
{
assert(pHead != NULL);
pList phead = *pHead;
while (phead->next != NULL)
{
phead = phead->next;
}
phead->next = *pHead;//建环
pList fast = *pHead;
pList slow = *pHead;
while (fast&&slow)
{
fast = fast->next->next;
slow = slow->next;
if (slow == fast)//快慢指针相遇
return 1;
}
return 0;
}
// 求环的长度
int GetCircleLen(pList* pHead)
{
assert(pHead != NULL);
int count = 0;
pList phead = *pHead;
pList cur = NULL;
while (phead->next != NULL)
{
phead = phead->next;
}
phead->next = *pHead;//建环
pList fast = *pHead;
pList slow = *pHead;
while (fast&&slow)
{
fast = fast->next->next;
slow = slow->next;
if (slow == fast)
{
cur = slow;//快慢指针相遇,标记相遇点
break;
}
}
while (cur->next != slow)
{
cur = cur->next;
count++;
}
return count + 1;
}
int CircleEnter(pList* pHead)//环的入口
{
assert(pHead);
int count = 0;
pList pre = *pHead;
pList cur = *pHead;
pList cur1 = *pHead;
pList fast = *pHead;
pList slow = *pHead;
while (cur->next != NULL)
{
cur = cur->next;
}
while (cur1->data != cur->data / 2)
{
cur1 = cur1->next;
}
cur->next = cur1;//创建一个带环的链表,并且环外也有链表,
while (fast&&slow)
{
fast = fast->next->next;
slow = slow->next;
if (slow == fast)
{
cur = slow;//找到相遇点,并且标记
break;
}
}
while (pre != cur)
{
pre = pre->next;//从头开始走
cur = cur->next;//从相遇点开始走
}
return cur->data;//当相遇得时候,就是入口点
}