mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 12:16:14 -04:00
28 lines
413 B
Python
28 lines
413 B
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
# author: bt3gl
|
|
|
|
|
|
class Node:
|
|
def __init__(self, x):
|
|
self.val = x
|
|
self.next = None
|
|
|
|
|
|
def detect_cycle(head):
|
|
|
|
seen = set()
|
|
node = head
|
|
|
|
while node is not None:
|
|
|
|
if node in seen:
|
|
return node
|
|
|
|
else:
|
|
seen.add(node)
|
|
node = node.next
|
|
|
|
return None
|
|
|