首页 > 编程语言 > 详细

[LeetCode] 141. Linked List Cycle 单链表判圆算法

时间:2019-05-31 12:39:29      阅读:90      评论:0      收藏:0      [点我收藏+]

TWO POINTER

快指针速度2 , 慢指针速度1

相对速度1,有环必然相遇

public class Solution {
    public boolean hasCycle(ListNode head) {
        ListNode fast = head,slow = head;
        while(fast!=null && fast.next!=null){
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast){
                return true;
            }
        }
        return false;
    }
}

 

[LeetCode] 141. Linked List Cycle 单链表判圆算法

原文:https://www.cnblogs.com/Poceer/p/10954550.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!