力扣 简单 141.环形链表
题目介绍
题解
思路:慢指针每次循环走一步,快指针每次走两步,快指针相对于慢指针每次多走一步(相对速度),如果有环的话,一步一步走肯定能遇到慢指针。
class Solution {
public boolean hasCycle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (fast == slow)
return true;
}
return false;
}
}
原文地址:https://blog.csdn.net/qq_51352130/article/details/143036633
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!