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:
- ListNode *detectCycle(ListNode *head) {
- ListNode *slow, *fast;
- slow = head;
- fast = head;
- bool cyclePresent = false;
- while(fast && fast->next){
- slow = slow->next;
- fast = fast->next->next;
- if(slow == fast){
- cyclePresent = true;
- break;
- }
- }
- if(!cyclePresent) return NULL;
- slow = head;
- while(slow != fast){
- slow = slow->next;
- fast = fast->next;
- }
- return slow;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement