-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect_cycle.py
49 lines (34 loc) · 955 Bytes
/
detect_cycle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Linked Lists: Detect a Cycle
https://www.hackerrank.com/challenges/ctci-linked-list-cycle
"""
import unittest
class Node(object):
def __init__(self, data=None, next_node=None):
self.data = data
self.next = next_node
def has_cycle(head):
short_mover = head
long_mover = head
while short_mover is not None or long_mover is not None:
for _ in range(2):
if long_mover is None:
return 0
long_mover = long_mover.next
short_mover = short_mover.next
if short_mover == long_mover:
return 1
return 0
class TestDetectCycle(unittest.TestCase):
def test_has_cycle(self):
head = Node(1)
self.assertEqual(
0,
has_cycle(head)
)
head = Node(1, Node(2, Node(3)))
head.next.next.next = head.next
self.assertEqual(
1,
has_cycle(head)
)