给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null
快慢指针
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def EntryNodeOfLoop(self, pHead):
# write code here
if pHead.next == None or pHead.next.next == None:
return None
f = pHead
s = pHead
while f.next.next and s.next:
f = f.next.next
s = s.next
if f == s:
temp = pHead
while temp != f:
temp = temp.next
f = f.next
if temp == f:
return temp
return None