mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 12:16:14 -04:00
30 lines
520 B
Python
30 lines
520 B
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
# author: bt3gl
|
|
|
|
class Node:
|
|
|
|
def __init__(self, val):
|
|
self.val = val
|
|
self.next = None
|
|
|
|
|
|
def has_cycle(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
|
|
|