From 74ea899d2cb4d0cfb174786265f55ed8712796c3 Mon Sep 17 00:00:00 2001 From: bumpsoo Date: Mon, 9 Jun 2025 14:53:03 +0000 Subject: [PATCH] https://leetcode.com/problems/linked-list-cycle --- easy/linked_list_cycle.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 easy/linked_list_cycle.py 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 +