Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for singly-linked list.
- * struct ListNode {
- * int val;
- * ListNode *next;
- * ListNode(int x) : val(x), next(NULL) {}
- * };
- */
- class Solution {
- public:
- bool hasCycle(ListNode *head) {
- ListNode *conejo = NULL;
- ListNode *tortuga = NULL;
- conejo = head;
- tortuga = head;
- bool firstTime = true;
- while(conejo && tortuga){
- if(conejo == tortuga && !firstTime)return true;
- firstTime = false;
- tortuga = tortuga->next;
- conejo = conejo->next;
- if(conejo) conejo = conejo->next;
- else return false;
- }
- return false;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement