1.用快慢指针判断是否有环
代码:
bool hasCycle(struct ListNode* head ) { // write code here struct ListNode *f=head; struct ListNode *s=head; while(f!=NULL){ if(f->next!=NULL) {f=f->next->next; s=s->next;} else{f=f->next;} if(s==f)return true; } return false; }