"""Definition of ListNodeclass ListNode(object): def __init__(self, val, next=None): self.val = val self.next = next"""classSolution:""" @param head: The first node of the linked list. @return: True if it has a cycle, or false """defhasCycle(self,head):# write your code hereifhead==Noneorhead.next==None:returnFalsefast_node=headslow_node=headwhilefast_node!=Noneandfast_node.next!=None:fast_node=fast_node.next.nextslow_node=slow_node.nextiffast_node==slow_node:returnTruereturnFalse