Create detect_cycle.py

This commit is contained in:
marina 2023-08-02 16:39:40 -07:00 committed by GitHub
parent 94db93ff42
commit 4e82aa6060
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,23 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
def has_cycle(self, head) -> bool:
if not head:
return False
p1 = head
p2 = head.next
while p1 != p2:
if not p1 or not p2 or not p2.next:
return False
p1 = p1.next
p2 = p2.next.next
return True