链表中环的入口节点

给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。

 1 # -*- coding:utf-8 -*-
 2 # class ListNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6 class Solution:
 7     def EntryNodeOfLoop(self, pHead):
 8         if pHead == None or pHead.next == None:
 9             return None
10         slow = pHead.next
11         fast = pHead.next.next
12         while slow != fast:
13             slow = slow.next
14             fast = fast.next.next
15         fast = pHead
16         while slow != fast:
17             slow = slow.next
18             fast = fast.next
19         return slow
20         # write code here

 

posted on 2019-06-14 09:54  Sempron2800+  阅读(111)  评论(0编辑  收藏  举报