Skip to main content

141. Linked List Cycle

Link

use slow and fast (pointer)indicator

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:

slow, fast = head , head

while slow:

slow = slow.next

if fast:
fast = fast.next
if fast:
fast = fast.next

if slow == fast:
break

return slow != None