"""
Definition of ListNode
class ListNode(object):
def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param head: The first node of linked list.
@return: The node where the cycle begins. if there is no cycle, returnnull"""
def detectCycle(self, head):
# write your code here
#带环的话,说明id地址刚好重复,则返回,否则的话返回null
if not head: return None
hashmap = {}
while head:
if (id(head) not in hashmap):
hashmap[id(head)] = True
else:
return head
head = head.next
return None