题解 | #链表中环的入口结点#
链表中环的入口结点
http://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def EntryNodeOfLoop(self, pHead):
# write code here
dict = {}
while pHead:
if pHead.val not in dict:
print(pHead.val)
dict[pHead.val] = 1
pHead = pHead.next
else:
return pHead
不管非环部分是不是为空如:{},{2},只是输入的时候人为的把环部分和非环部分分开了,在遍历的时候都是2还是第一个节点。