题解 | #链表中环的入口结点#

链表中环的入口结点

https://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    def EntryNodeOfLoop(self, pHead):
        # write code here
        slow = pHead 
        fast = pHead 
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            # 找到快慢指针相遇的点
            if slow == fast:
                index1 = fast
                index2 = pHead
                # 利用数学推导 当 x = z 时即链表中环的入口节点
                while index1!=index2:
                    index1 = index1.next
                    index2 = index2.next
                return index1
        return None

数学推导,当x = z 时即为环入口

全部评论

相关推荐

每晚夜里独自颤抖:你cet6就cet6,cet4就cet4,你写个cet证书等是什么意思。专业技能快赶上项目行数,你做的这2个项目哪里能提现你有这么多技能呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务