/**
 * 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* p1 = head;
        while (p1 && p1->next) {
            head = head->next;
            p1 = p1->next->next;
            if (head==p1) return true;
        }
        return false;
    }
};