diff --git a/easy/linked_list_cycle.py b/easy/linked_list_cycle.py new file mode 100644 index 0000000..7dd4325 --- /dev/null +++ b/easy/linked_list_cycle.py @@ -0,0 +1,23 @@ +# https://leetcode.com/problems/linked-list-cycle + +from typing import Optional + +class ListNode: + val: int + next: Optional["ListNode"] + +class Solution: + def hasCycle(self, head: Optional[ListNode]) -> bool: + first: Optional[ListNode] = head + second: Optional[ListNode] = head + + while first is not None and \ + first.next is not None and \ + second is not None: + + first = first.next.next + second = second.next + if first == second: + return True + return False +