/*
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;
if ( head == NULL ) return 0 ;
if ( fast- > next == NULL || fast- > next- > next == NULL ) return 0 ;
fast = fast- > next- > next;
while ( slow && fast)
{
if ( slow == fast)
return 1 ;
slow = slow- > next;
fast = fast- > next- > next;
}
return 0 ;
}
LyoKICBEZXRlY3QgbG9vcCBpbiBhIGxpbmtlZCBsaXN0IAogIExpc3QgY291bGQgYmUgZW1wdHkgYWxzbwogIE5vZGUgaXMgZGVmaW5lZCBhcyAKICBzdHJ1Y3QgTm9kZQogIHsKICAgICBpbnQgZGF0YTsKICAgICBzdHJ1Y3QgTm9kZSAqbmV4dDsKICB9CiovCmludCBIYXNDeWNsZShOb2RlKiBoZWFkKQp7CiAgICBOb2RlICpzbG93ID0gaGVhZDsKICAgIE5vZGUgKmZhc3QgPSBoZWFkOwogICAgCiAgICBpZihoZWFkID09IE5VTEwpIHJldHVybiAwOwogICAgaWYoZmFzdC0+bmV4dCA9PSBOVUxMIHx8IGZhc3QtPm5leHQtPm5leHQgPT0gTlVMTCkgcmV0dXJuIDA7CiAgICAKICAgIGZhc3QgPSBmYXN0LT5uZXh0LT5uZXh0OwogICAgd2hpbGUoc2xvdyAmJiBmYXN0KQogICAgewogICAgICAgIGlmKHNsb3cgPT0gZmFzdCkKICAgICAgICAgICAgcmV0dXJuIDE7CiAgICAgICAgc2xvdyA9IHNsb3ctPm5leHQ7CiAgICAgICAgZmFzdCA9IGZhc3QtPm5leHQtPm5leHQ7CiAgICB9CiAgICByZXR1cm4gMDsKfQ==
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