diff --git a/linked_lists/detect_cycle.py b/linked_lists/detect_cycle.py new file mode 100644 index 0000000..ebd28ed --- /dev/null +++ b/linked_lists/detect_cycle.py @@ -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 +