题解 | #链表中环的入口结点#
链表中环的入口结点
http://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4
采用集合的方式解决
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def EntryNodeOfLoop(self, pHead):
# write code here
set1 = set() ### 一个集合
while pHead:
if pHead.val not in set1: #### 如果不在,则添加
set1.add(pHead.val)
pHead = pHead.next
else: ### 如果存在说明是第二次出现,那必须是环的入口,因为只有环的入口才是第一个存在重复的结点
return pHead