70,022
社区成员




typedef struct LNode
{
int data;
LNode *next;
}LNode ;
typedef LNode* LinkList;
//单链表有环返回true 否则返回false
bool is_looplist(LNode *head)
{
LNode *fast,*slow;
if (head == NULL || head->next == NULL)
{
return false;
}
fast = slow = head;
while(true)
{
if(!fast || !fast->next)
return false;
//为了防止fast跨过slow的情况,在每次判断的时候比较当前节点和下一节点
else if (fast == slow || fast->next == slow)
return true;
else
{
slow = slow->next;//一次跳一步
fast = fast->next->next;//一次跳两步
}
}
}
bool is_looplist(LNode *head)
{
LNode *fast,*slow;
if (head == NULL || head->next == NULL)
{
return false;
}
fast = slow = head;
while(true)
{
if(!fast || !fast->next)
return false;
//为了防止fast跨过slow的情况,在每次判断的时候比较当前节点和下一节点
else if (fast == slow || fast->next == slow)
return true;
else
{
slow = slow->next;//一次跳一步
fast = fast->next->next;//一次跳两步
}
}
}