Skip to content

Instantly share code, notes, and snippets.

@kanrourou
Created December 25, 2018 05:27
Show Gist options
  • Save kanrourou/355570b1d2fabf689e93d744c45a5800 to your computer and use it in GitHub Desktop.
Save kanrourou/355570b1d2fabf689e93d744c45a5800 to your computer and use it in GitHub Desktop.
/**
* 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) {
auto slow = head, fast = head;
while(fast && fast->next)
{
fast = fast->next->next;
slow = slow->next;
if(fast == slow)return true;
}
return false;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment