怎么判断链表有环,怎么找环节点
一、快慢指针法(判断是否有环)
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; } }