【Java - J - 23】链表中环的入口节点

题目描述

给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。

练习地址

L - 0141】- e - 环形链表

实现

判断是否有环

  1. p1,p2同时从头节点出发
  2. p1每次走一步,p2每次走两步
  3. 快的追上慢的,有环

环的入口

  1. 初始化:p1,p2都指向头节点
  2. 环有n个节点,p1先走n步
  3. p1,p2再同时走
  4. 相遇的节点便是入口
/**23 链表中环的入口节点*/
public class C23_list_EntryNodeOfLoop {
    static ListNode getMeetNode(ListNode head) {//快慢指针相遇的点
        if (head == null) { return null; }
        ListNode slow = head.next; //慢指针
        if (slow == null) { return null; }
        ListNode fast = slow.next; //快指针
        while (fast != null && slow != null) {
            if (fast == slow) { //相遇的点
                return fast;
            }
            slow = slow.next;
            fast = fast.next;
            if (fast != null) { fast = fast.next; } //存在环
        }
        return null;
    }
    //找到环中任意节点,就可以得出环中节点数目,并找到入口
    public static ListNode EntryNodeOfLoop(ListNode head) {
        ListNode meetNode = getMeetNode(head);
        if (meetNode == null) return null;
        //获得环中节点数目
        int count = 1;
        ListNode node1 = meetNode;
        while (node1.next != meetNode) {
            node1 = node1.next;
            ++count;
        }
        //先移动node1,次数为环中节点的数目
        node1 = head;
        for (int i = 0; i < count; i++) {
            node1 = node1.next;
        }
        //再移动node1和node2
        ListNode node2 = head;
        while (node1 != node2) {
            node1 = node1.next;
            node2 = node2.next;
        }
        return node1;
    }
}
Test
 public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);
        ListNode node6 = new ListNode(6);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;
        node6.next = node3;
        System.out.println(EntryNodeOfLoop(node1).val);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值