141. Linked List Cycle

Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?

哈希表求解

简单处理就是将数据存放到哈希表,遍历的过程中判断是否存在,若存在说明有环。但是此算法在空间复杂度和时间复杂度上均为 O(n) ,性能不佳。代码如下:

public class Solution {
    public boolean hasCycle(ListNode head) {
        Set<ListNode> set = new HashSet<ListNode>();
        ListNode temp = head;
        while(temp != null) {
            if(set.contains(temp)) 
                return true;
            else 
                set.add(temp);
            temp = temp.next;
        }
        return false;
    }
}

赛跑算法

也就是Cycle Detection,具体可参考Floyd判圈算法
Copy 了一段外国人的清晰解释:

Since the runner’s speed is faster than walker, then it is guaranteed that runner will pass walker in some time. The only thing we need to prove is that the runner never jumps over walker so never meet. Suppose the runner jumps over walker in one unit of time, then we need to guarantee that 2 > distance + 1. So distance < 1 so distance = 0. This implies that runner already meets walker, contradiction. So runner will never jumps over walker.
Runner will pass walker + runner never jumps over = runner will meet walker.

时间复杂度不变,但是空间复杂度下降到了 O(1) 。算法如下:

public class Solution {
    public boolean hasCycle(ListNode head) {
        if(head == null || head.next == null) return false;
        ListNode slow = head;
        ListNode fast = head;
        //由于fast遍历速度较快
        //所以只判断fast有没有下个节点和下下个节点
        while(fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast) {
                return true;
            }
        }
        return false;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值