12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- from typing import Optional
- import pytest
- from utils import ListNode, arr_to_linklist
- def hasCycle(head: Optional[ListNode]) -> bool:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if not head or not head.next:
- return False
- slow = head
- fast = head.next
- while fast != slow:
- if not fast or not fast.next:
- return False
- slow = slow.next
- fast = fast.next.next
- return True
- @pytest.mark.parametrize(
- "head, expect",
- [
- ([3,2,0,-4], True)
- ]
- )
- def test_cases(head, expect):
- assert hasCycle(arr_to_linklist(head)) == expect
|