Given single linked list find if it has a circle.
Answers and Comments
bool IsCircular(LList* pHead)
{
bool bCIrc = false;
LList* p1 = pHead;
LList* p2 = pHead;
while (pr1)
{
p1 = p1->next;
p2 = p2->next;
if (p2)
p2=p2->next;
if (p1 == p2)
{
bCirc = true;
break;
}
}
return bCirc;
}
There is additional question to the original problem:
To find a joint pont.
{
bool bCIrc = false;
LList* p1 = pHead;
LList* p2 = pHead;
while (pr1)
{
p1 = p1->next;
p2 = p2->next;
if (p2)
p2=p2->next;
if (p1 == p2)
{
bCirc = true;
break;
}
}
return bCirc;
}
There is additional question to the original problem:
To find a joint pont.
Saved Stories
Sponsored Categories
public bool hasLoop(Node head)
{
Node first=null;
Node second=null;
Node tmp=head;
while(tmp)
{
first=second;
second=tmp;
tmp=tmp.Next;
if(tmp==head) return true;
second.Next=first;
}
return false;
}