Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
class Solution:
def hasCycle(self, head: ListNode) -> bool:
T:O(n) If there's no cycle, apparently, it's O(n). If there's cycle, O(N+K), N is non-cyclic length and K is alsmot cyclic lengh, in total is O(n).
K comes from how many iterations we needed for xxxx==it's logest distance between 2 runners / difference of speed. So, it's almost K/1
return self.detect_cycle(head)
def detect_cycle(self, head):
if not head:
return False
s = f = head
while f and
s =
f =
if s==f:
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment