-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2.5_find_loop.py
More file actions
49 lines (36 loc) · 837 Bytes
/
2.5_find_loop.py
File metadata and controls
49 lines (36 loc) · 837 Bytes
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
from linked_list import Node, LinkedList
def find_loop(ll):
if (ll is None) or (ll.head is None):
return None # no loop
fast = ll.head
slow = ll.head
loop = None
while (fast is not None) and (slow is not None):
if (slow.next is None) or (fast.next is None) or (fast.next.next is None):
return None # no loop
slow = slow.next
fast = fast.next.next
if fast == slow:
fast = ll.head
while fast != slow:
fast = fast.next
slow = slow.next
return slow
return None
def make_loop(rng, data):
ll = LinkedList(rng)
x = None
y = None
n = ll.head
while True:
if n.data == data:
x = n
if n.next == None:
y = n
break
n = n.next
y.next = x
return ll
for i in range(1, 9):
l = find_loop(make_loop([1, 2, 3, 4, 5, 6, 7, 8], i))
print str(i) + ': loop in ' + str(l.data)