Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- knakul853
- */
- class Solution {
- public:
- ListNode *detectCycle(ListNode *head) {
- ListNode* slow = head;
- ListNode* fast = head;
- bool has = false;
- while(fast && fast->next )
- {
- fast = fast->next->next;
- slow = slow->next;
- if(slow == fast){ has = true; break;}
- }
- if(!has) return NULL;
- slow = head;
- while(fast != slow )
- {
- fast = fast->next;
- slow = slow->next;
- }
- return slow;
- }
- };
Add Comment
Please, Sign In to add comment