fork(1) 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. if(head == NULL) return 0;
  17. if(fast->next == NULL || fast->next->next == NULL) return 0;
  18.  
  19. fast = fast->next->next;
  20. while(slow && fast)
  21. {
  22. if(slow == fast)
  23. return 1;
  24. slow = slow->next;
  25. fast = fast->next->next;
  26. }
  27. return 0;
  28. }
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