## 链表遍历 + set判重
* 对链表进行遍历,通过数组来记录,该节点是否出现,如果出现,则这个节点就是链表的环形的入口
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
unordered_set<ListNode*> st;
bool hasCycle(ListNode *head) {
if(head == NULL) return 0;
while(head){
if(st.count(head)) return 1;
st.insert(head);
head=head->next;
}
return 0;
}
};

京公网安备 11010502036488号