环形链表

/**
* 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) {
unordered_set<ListNode*> c;
while(head != NULL){
if(c.count(head)){
return true;
}
c.insert(head);
head = head -> next;
}
return false;
}
};

浙公网安备 33010602011771号