题解 | #判断链表中是否有环#
判断链表中是否有环
https://www.nowcoder.com/practice/650474f313294468a4ded3ce0f7898b9
#include <stdbool.h>
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
*
* @param head ListNode类
* @return bool布尔型
*/
bool hasCycle(struct ListNode* head ) {
// write code here
//struct ListNode* p;
int e = 0;
//if(head==NULL) return true;
//对于有环节点,如果遍历链表会无限循环,设定一个循环次数,超过这个次数视为有环
while(head){
e++;
head=head->next;
if(e>=10000){
return true;
}
}
return false;
}


查看5道真题和解析