kucheasysa

Algoverse_adesh_36

Jul 5th, 2024
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.48 KB | None | 0 0
  1. # Definition for singly-linked list.
  2. # class ListNode:
  3. #     def __init__(self, x):
  4. #         self.val = x
  5. #         self.next = None
  6.  
  7. class Solution:
  8.     def hasCycle(self, head: Optional[ListNode]) -> bool:
  9.         if not head:
  10.             return False
  11.         slow = head
  12.         fast = head.next
  13.         while fast and fast.next:
  14.             if slow == fast:
  15.                 return True
  16.             slow = slow.next
  17.             fast = fast.next.next
  18.  
  19.         return False
Advertisement
Add Comment
Please, Sign In to add comment