一种方法是用set存储出现过的指针,重复出现时就是有环:

 1 class Solution {
 2 public:
 3     bool hasCycle(ListNode *head) {
 4         set<ListNode*> st;
 5         ListNode *p = head;
 6         while(p) {
 7             if (st.find(p) != st.end())
 8                 return true;
 9             st.insert(p);
10             p = p->next;
11         }
12         return false;
13     }
14 };
View Code

相关文章:

  • 2021-07-08
  • 2021-08-08
  • 2021-09-22
  • 2022-03-08
  • 2022-02-11
  • 2021-12-16
猜你喜欢
  • 2022-01-09
  • 2021-04-23
  • 2021-09-02
  • 2021-09-14
  • 2021-12-05
  • 2022-02-02
  • 2022-01-05
  • 2021-10-04
相关资源
相似解决方案