class Node(object): def __init__(self,value): self.value = value self.nextnode = None a = Node(1) b = Node(2) c = Node(3) d = Node(4) e = Node(5) x = Node(10) y = Node(20) z = Node(30) a.nextnode = b b.nextnode = c c.nextnode = d d.nextnode = e e.nextnode = c x.nextnode = y y.nextnode = z def cycle_check_any(node): slow_p = node fast_p = node while fast_p !=None and fast_p.nextnode !=None: slow_p = slow_p.nextnode fast_p = fast_p.nextnode.nextnode if fast_p == slow_p: return True return False ########################## result = cycle_check_any(a) print(result) result = cycle_check_any(x) print(result)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question