题目描述
给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
说明:本题目包含复杂数据结构ListNode,点此查看相关信息
# -*- coding:utf-8 -*-# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution:def EntryNodeOfLoop(self, pHead):# write code hereif not pHead or not pHead.next:return Nonep_slow = pHeadp_fast = pHeadwhile p_slow and p_fast.next:p_slow = p_slow.nextp_fast = p_fast.next.nextif p_slow == p_fast:breakp1 = pHeadp2 = p_slowwhile p1!=p2:p1 = p1.nextp2 = p2.nextreturn p1
