链表中环的入口结点

2018-04-04  本文已影响0人  GoDeep

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

# -*- 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
上一篇下一篇

猜你喜欢

热点阅读