-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop_detection.py
56 lines (41 loc) · 1.15 KB
/
loop_detection.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
50
51
52
53
54
55
56
# 2.8: Loop Detection
from linked_list import LinkedList, Node
# Runtime: O(n) - Space: O(1)
def loop_detection(node: Node) -> Node:
nodes = set()
while node:
if node in nodes:
return node
nodes.add(node)
node = node.next
def loop_detection2(node: Node) -> Node:
slow = node
fast = node
# loop through until they meet at some collision point
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
break
# error handling to ensure that there's an actual
# corrupt linked list
if not fast or not fast.next:
return None
# reset slow to head and move them at same pace
slow = head
while slow != fast:
slow = slow.next
fast = fast.next
# return corrupt node (either works)
return fast
circular = Node("C")
head = Node("A")
head.next = Node("B")
head.next.next = circular
circular.next = Node("D")
circular.next.next = Node("E")
circular.next.next.next = circular
print("Chapter 2.8")
print(loop_detection(head).data)
print("Chapter 2.8 using fast/slow refs")
print(loop_detection2(head).data)