一、快慢指针法(判断是否有环)
int has_cycle(node *head) { if (head == NULL) return false; node* fast = head; node* slow = head; while (1) { if (slow->next != NULL) slow = slow->next; //慢指针走一步 else return false; if (fast->next != NULL && fast->next->next != NULL) fast = fast->next->next; //快指针走两步 else return false; if (slow == fast) return true; } }
原文:https://www.cnblogs.com/zl222333zl/p/12488803.html