剑指Offer——有环链表的入口点

那么多学技术的都可以成功,凭什么我不行

有环链表的入口点

本文基于《剑指Offer》 使用Java代码实现

更多《剑指Offer》Java实现合集

目录

 

题目 

思路

解法1代码实现

测试代码

 解法二

收获


题目 

  一个链表中包含环,如何找出环的入口结点?

 

思路

  1.确定链表是否有环:通过两个不同速度的指针确定,当两个指针指向同一个结点时,该结点为环中的一个结点。

  2.确定环中结点的数目n:指针走一圈,边走边计数

  3.找到环的入口:从头结点开始,通过两个相差为n的指针来得到(即寻找链表中倒数第n个结点)

解法1代码实现

/**
 * @ClassName: EntryNodeInListLoop
 * @description: 链表中环的入口点
 * @author: XZQ
 * @create: 2020/4/27 15:10
 **/
public class EntryNodeInListLoop {
    /*
     * 确定链表是否有环,采用快慢指针确定
     * 返回值代表快慢指针相遇时的结点,返回null代表链表无环
     */
    private ListNode meetingNode(ListNode head) {

        ListNode pSlow = head;//慢指针
        ListNode pFast = head;//快指针

        while (pFast != null && pFast.next != null) {
            pFast = pFast.next.next;//快指针每次移动两个Node
            pSlow = pSlow.next;
            if (pFast == pSlow) {//如果相遇,返回相遇点,相遇点一定在环中
                return pFast;
            }
        }
        return null;
    }


    /**
     * 计算环中入口结点
     */
    public ListNode entryNodeOfLoop(ListNode head) {
        /*先找到环的长度 从相遇点开始循环直到下次到达相遇点即环长*/
        ListNode meetingNode = meetingNode(head);

        int length = 1;
        ListNode node = meetingNode.next;
        if (meetingNode != null) {
            while (node != meetingNode) {
                length++;
                node = node.next;
            }
        }

        /*从头结点开始,通过两个相差为环长length的指针来得到入环点*/
        ListNode node2 = head;
        for (int i = 0; i < length; i++) {
            node2 = node2.next;
        }
        node = head;
        while (node != node2) {
            node = node.next;
            node2 = node2.next;
        }
        return node;
    }

}


class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}

测试代码

    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);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node2;

        EntryNodeInListLoop entryNodeInListLoop = new EntryNodeInListLoop();

        ListNode node = entryNodeInListLoop.meetingNode(node1);
        System.out.println("是否是有环链表: "
                + (node == null ? "False" : "Yes,the node is :" + node.val + "入环点是"
                + entryNodeInListLoop.entryNodeOfLoop(node1).val));
    }

 解法二

The distance between the head node and entry node equals the distance between the meeting node and entry node

头节点与进入节点之间的距离等于相遇点与进入节点之间的距离

public ListNode detectCycle(ListNode head) {
        if (head == null)
            return null;

        ListNode fast = head;
        ListNode slow = head;
        ListNode entry = head;

        while (fast.next != null && fast.next.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {//相遇
                while (slow != entry) {
                    slow = slow.next;
                    entry = entry.next;
                }
                return entry;
            }
        }
        return null;
    }

收获

  1.通过两个不同速度的指针可以确定链表中是否有环

  2.相差n步的两个指针可以找到倒数第n个结点

  3.复杂问题分解成为几个简单问题(本题分为三步:找出环中任一结点;得到环的个数;找到入口结点)

 更多《剑指Offer》Java实现合集

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值