代码
#include <iostream>
using namespace std;
struct link {
int data;
link *next;
};
bool isLoop(link *head) {
link *p1 = head, *p2 = head;
if (head == NULL || head->next == NULL) {
return false;
}
do {
p1 = p1->next;
p2 = p2->next->next;
} while (p2 && p2->next && p1 != p2);
return p1 == p2;
}