fork download
  1. /*
  2.   Detect loop in a linked list
  3.   List could be empty also
  4.   Node is defined as
  5.   struct Node
  6.   {
  7.   int data;
  8.   struct Node *next;
  9.   }
  10. */
  11. int HasCycle(Node* head)
  12. {
  13. Node *slow = head;
  14. Node *fast = head;
  15.  
  16. while(slow && fast && fast->next)
  17. {
  18. slow = slow->next;
  19. fast = fast->next->next;
  20. if(slow == fast)
  21. return 1;
  22. }
  23. return 0;
  24. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
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
Standard output is empty