Linked List Cycle
Example
Solution
"""
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 the linked list.
@return: True if it has a cycle, or false
"""
def hasCycle(self, head):
# write your code here
if head == None or head.next == None:
return False
slow, fast = head, head.next
while fast and fast.next:
if fast == slow:
return True
slow = slow.next
fast = fast.next.next
return FalseLast updated