判断链表中是否有环 返回环的入口结点 (快慢指针)

    class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
          val = x;
          next = null;
        }
    }

判断是否有环

set集合法 (时间复杂度O(n), 空间复杂度O(n))

    public boolean hasCycle(ListNode head) {
        Set<ListNode> set = new HashSet<>();

        while (head != null){
            if(set.contains(head))
                return true;
            set.add(head);
            head = head.next;
        }

        return false;
    }

快慢指针法(时间复杂度O(n),空间复杂度O(1))

    /**
     * 快慢指针,快指针 fast 每次走两步,慢指针 slow 每次走一步
     *
     *  如果链表没有环,那么两个指针肯定无法相遇,fast指针走到 null,循环结束,返回 false
     *  如果链表有环,那么两个指针最终会相遇,相遇就返回 true
     *
     *  时间复杂度为 O(n),空间复杂度为 O(1)
     *
     * @param head
     * @return
     */
    public boolean hasCycle(ListNode head) {
        ListNode slow = head, fast = head;

        while (fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;

            if(slow == fast)
                return true;
        }

        return false;
    }

逐个删除法 (时间复杂度O(n),空间复杂度O(1))

    /**
     * 逐个删除, 删除的结点的 next 指向它自己
     *      如果没有环, 则不会遇到 next 指向它自己的结点
     *      如果有环,则必定会遇到 next 指向它自己的结点
     * @param head
     * @return
     */
    public boolean hasCycle(ListNode head) {
        ListNode delNode;

        while (head != null){
            if(head == head.next)
                return true;
            delNode = head;
            head = head.next;
            delNode.next = delNode;
        }

        return false;
    }

返回环的入口结点

快慢指针法

在这里插入图片描述

  • 慢结点 slow 走过的结点数为 a + b
  • 快结点 fast 走过的结点数为 a + n * (b + c)
  • 由于我们设置慢结点每次走一步,快结点每次走两步,所以有 2 * (a + b) = a + n * (b + c) ,则有 a = (n - 1) * (b + c) + c,所以当两个结点相遇后,我们可以让其中一个节点回到头结点,然后两个结点以相同的速度前进,最终会在环的入口结点相遇
    /**
     * 双指针,快慢指针
     * @param head
     * @return
     */
    public ListNode detectCycle(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;

        while (fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;

            if(slow == fast){
                slow = head;
                while (slow != fast){
                    slow = slow.next;
                    fast = fast.next;
                }
                return fast;
            }
        }

        return null;
    }

逐个删除法

    /**
     *    遍历过的结点的next指向它自己,
     *       若没有环,则最终head会走到null,
     *       否则,head走到一个next指向它自己的结点,
     *       且该结点就是环的入口结点
     * @param head
     * @return
     */
    public ListNode detectCycle(ListNode head) {


        while (head != null){
            if(head.next == head)
                return head;
            ListNode temp = head.next;
            head.next = head;
            head = temp;
        }

        return null;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值