leetcode 之Linked List Cycle(24)

两个思路,一是用哈希表记录每个结点是还被访问过;二是定义两个快、慢指针,如果存在环的话,两个指针必定会在某位结点相遇。

bool linkListNode(ListNode *head)
      {
          ListNode *fast=head, *slow=head;
          while (fast && fast->next)
          {
              slow = slow->next;
              fast = fast->next->next;

              if (fast == slow)return true;

          }

          return false;
      }
View Code
原文地址:https://www.cnblogs.com/573177885qq/p/5514926.html