//快慢指针的应用解决判断链表中是否有环的问题,若有环快慢指针必定相遇
bool hasCycle(struct ListNode *head) {
struct ListNode *fast = head;
struct ListNode *slow = head;
while(fast && fast->next){
slow = slow->next;
fast = fast -> next -> next;
if(slow == fast)
return true;
}
return false;
}
今天第一次做LeetCode,也是第一次发CSDN,记录一下life
思路是新学的,第一次听说快慢指针,还是自己太菜了,以后记住这个方法 吧