【java版数据结构】找到环形链表的入口

原理:我们先判断当前链表是否是环形链表,定位到快慢指针相遇的结点,这是创建一个temp指针指向第一个元素,用temp指针和慢指针继续遍历链表,temp指针和慢指针相遇的结点就是环形链表的入口

数学分析:
为什么在快慢指针相遇之后,用temp指针和慢指针去遍历链表时,相遇的结点就是环形链表的入口呢?

在这里插入图片描述

    //找出循环链表的入口
    public static Node getEntrance(Node first){
        //判断链表是否有环
        //定义两个快慢指针
        Node fast=first;
        Node slow=first;
        Node temp=null;
        //遍历链表,判断两指针是否相遇
        while(slow!=null&&slow.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            //相遇,让新指针指向第一个结点,找到slow指针和temp指针相遇的结点,就是环的入口
            if(fast.equals(slow)){
                temp=first;
                continue;
            }
            //是循环链表
            if(temp!=null){
                temp=temp.next;
                //相遇了,找到了环的入口
                if(slow.equals(temp)){
                    break;
                }
            }
        }
        //返回循环链表的入口
        return temp;
    }

测试代码:

package com.company.test;

/**
 * @ProjectName 数据结构实操
 * @ClassName FastSlowTest
 * @Description // 快慢指针,解决中间值问题
 * @Email 2992794262@qq.com
 * @Author ASUS
 * @Date 2022/1/7
 **/
public class CycleLinkEntranceTest {
    public static void main(String[] args) {
        Node first=new Node("aa",null);
        Node second=new Node("bb",null);
        Node third=new Node("cc",null);
        Node fourth=new Node("dd",null);
        Node fifth=new Node("ee",null);
        Node sixth=new Node("ff",null);
        Node seventh=new Node("gg",null);


        first.next=second;
        second.next=third;
        third.next=fourth;
        fourth.next=fifth;
        fifth.next=sixth;
        sixth.next=seventh;

        //环形链表
        seventh.next=third;
        Node entrance = getEntrance(first);
        System.out.println("环的入口是:"+entrance.item);

    }

    //找出循环链表的入口
    public static Node getEntrance(Node first){
        //判断链表是否有环
        //定义两个快慢指针
        Node fast=first;
        Node slow=first;
        Node temp=null;
        //遍历链表,判断两指针是否相遇
        while(slow!=null&&slow.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            //相遇,让新指针指向第一个结点,找到slow指针和temp指针相遇的结点,就是环的入口
            if(fast.equals(slow)){
                temp=first;
                continue;
            }
            //是循环链表
            if(temp!=null){
                temp=temp.next;
                //相遇了,找到了环的入口
                if(slow.equals(temp)){
                    break;
                }
            }
        }
        //返回循环链表的入口
        return temp;

    }

    private static class Node<T>{
        T item;
        Node next;

        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值