题目描述
给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
说明:本题目包含复杂数据结构ListNode,点此查看相关信息
# -*- 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 not pHead or not pHead.next:
return None
p_slow = pHead
p_fast = pHead
while p_slow and p_fast.next:
p_slow = p_slow.next
p_fast = p_fast.next.next
if p_slow == p_fast:
break
p1 = pHead
p2 = p_slow
while p1!=p2:
p1 = p1.next
p2 = p2.next
return p1