链表中环的入口结点

题目描述
一个链表中包含环,请找出该链表的环的入口结点。

# -*- coding:utf-8 -*-
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
        
class Solution:
    def EntryNodeOfLoop(self, pHead):
        # write code here
        fast,slow=pHead,pHead
        while fast and fast.next:
            fast=fast.next.next
            slow=slow.next
            if fast==slow:
                fast=pHead
                while fast!=slow:
                    fast=fast.next
                    slow=slow.next
                return fast

你可能感兴趣的:(链表中环的入口结点)