mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Create detect_cycle.py
This commit is contained in:
parent
94db93ff42
commit
4e82aa6060
23
linked_lists/detect_cycle.py
Normal file
23
linked_lists/detect_cycle.py
Normal 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user