public class Solution {
public boolean hasCycle(ListNode head) {
/*
链表有环思路:如果有环,设置一个快指针,设置一个慢指针,
快指针一次走两步,慢指针一次走一步,快指针总能追上慢的
*/
if(head == null) return false;
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if(fast == slow) {
return true;
}
}
return false;
}
}
牛客网算法题:判断链表中是否有环
猜你喜欢
转载自blog.csdn.net/weixin_42228950/article/details/109479599
今日推荐
周排行