/*
Detect loop in a linked list
List could be empty also
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
int HasCycle( Node* head)
{
Node * slow = head;
Node * fast = head;
while ( slow && fast && fast- > next)
{
slow = slow- > next;
fast = fast- > next- > next;
if ( slow == fast)
return 1 ;
}
return 0 ;
}
LyoKICBEZXRlY3QgbG9vcCBpbiBhIGxpbmtlZCBsaXN0IAogIExpc3QgY291bGQgYmUgZW1wdHkgYWxzbwogIE5vZGUgaXMgZGVmaW5lZCBhcyAKICBzdHJ1Y3QgTm9kZQogIHsKICAgICBpbnQgZGF0YTsKICAgICBzdHJ1Y3QgTm9kZSAqbmV4dDsKICB9CiovCmludCBIYXNDeWNsZShOb2RlKiBoZWFkKQp7CiAgICBOb2RlICpzbG93ID0gaGVhZDsKICAgIE5vZGUgKmZhc3QgPSBoZWFkOwogICAgCiAgICB3aGlsZShzbG93ICYmIGZhc3QgJiYgZmFzdC0+bmV4dCkKICAgIHsKICAgICAgICBzbG93ID0gc2xvdy0+bmV4dDsKICAgICAgICBmYXN0ID0gZmFzdC0+bmV4dC0+bmV4dDsKICAgICAgICBpZihzbG93ID09IGZhc3QpCiAgICAgICAgICAgIHJldHVybiAxOwogICAgfQogICAgcmV0dXJuIDA7Cn0=
compilation info
prog.cpp:11:14: error: 'Node' was not declared in this scope
int HasCycle(Node* head)
^
prog.cpp:11:20: error: 'head' was not declared in this scope
int HasCycle(Node* head)
^
prog.cpp:12:1: error: expected ',' or ';' before '{' token
{
^
stdout