详解环形链表

LeetCode链接

【题目描述】

给定一个链表的头节点  head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null

实例1:有链表3-->2-->0-->-4-->2,可以一眼看出其内部是有环的。

 但是用代码如何判断有环? 如何找到开始入环的第一个节点?

【第一步判断有环】

因为有环的存在,链表像是两个人围绕着操场顺时针跑步。什么时候两个人能遇见当然是一个人速度大于另一个人的速度。

因此我们定义两个指针,一个fast指针每次移动两个节点,一个slow指针每次移动一个节点,当两个节点相遇时候就能够说明链表是有环的了。


    public static ListNode detectCycle(ListNode head) {
        // 定义快慢指针,快指针每次走两步,慢指针每次走一步
        ListNode slow = head;
        ListNode fast = head;

        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next;
            fast = fast.next;
            // 说明有环
            if (slow == fast) {
                // 找到入环的第一个节点
            }
        }
        return null;
    }

【找到入环的第一个节点】

设,从起始点到换入口点的距离是x,入口点到相遇的距离是y,相遇到出口点距离为z

slow节点走过的距离为x+y,fast节点走过的距离为x+y+n(y+z),其中n(y+z)表示fast在其中转了很多圈了然后才相遇

那么就有 (x + y )*2 = x+y+n(y+z) 

得x = (n-1)*y +n*z

【表明】如果再有一个指针index 从head处出发,fast从相遇位置出发,他们每次都走一步,则相遇的位置就是入口节点。

 【完整代码】

package link;

public class DetectCycle {
    public static void main(String[] args) {
        ListNode node1 = new ListNode(3);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(0);
        ListNode node4 = new ListNode(4);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        // 形成环状
        node4.next = node2;
        ListNode node = detectCycle(node1);
        System.out.println("环的入口:" + node);
    }

    public static ListNode detectCycle(ListNode head) {
        if (head == null) {
            return null;
        }
        // 定义快慢指针,快指针每次走两步,慢指针每次走一步
        ListNode slow = head;
        ListNode fast = head;

        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next;
            fast = fast.next;
            // 说明有环
            if (slow == fast) {
                ListNode index1 = fast;
                ListNode index2 = head;
                while (index1 != index2) {
                    index1 = index1.next;
                    index2 = index2.next;
                }
                return index2;
            }
        }
        return null;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兜兜转转m

一毛钱助力博主实现愿望

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值