From 41b70c839db7731d2a51eba9e17311b6127e9340 Mon Sep 17 00:00:00 2001 From: Yuseok Jo Date: Wed, 19 Aug 2026 23:43:58 +0900 Subject: [PATCH] linked list cycle solution --- linked-list-cycle/yuseok89.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 linked-list-cycle/yuseok89.py diff --git a/linked-list-cycle/yuseok89.py b/linked-list-cycle/yuseok89.py new file mode 100644 index 0000000000..6d956e6910 --- /dev/null +++ b/linked-list-cycle/yuseok89.py @@ -0,0 +1,22 @@ +# TC: O(N) +# SC: O(1) +# 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: + if not head or not head.next: + return False + + slow, fast = head, head.next + while slow != fast: + if not fast or not fast.next: + return False + + slow, fast = slow.next, fast.next.next + + return True +