算法:让快慢两个指针从链表的头元素出发开始遍历,无限循环 如果快指针遇到了“NULL”指针,返回该链表以“NULL”结束,是一个非循环链表,如果快指针追上或者超过了慢指针,返回该链表是一个循环链表,让慢指针前进一个结点,让快指针前进两个结点。
int determineTermination(node *head)
{
if(!head) return 0;
node *fast, *slow;
slow=head;
fast=head->next;
while(fast!=NULL && fast->next!=NULL){
if (fast == slow || fast->next == slow)
return 1;
slow = slow->next;
fast = fast->next->next;
}
}
转自:http://blog.163.com/shi_shun/blog/static/237078492010355211500/