算法1
(Hash法)
时间复杂度:O(n)
空间复杂度: O(n)
python3 代码
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def entryNodeOfLoop(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
visited = set()
p = head
while p:
if p in visited:
return p
else:
visited.add(p)
p = p.next
return None