forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
first_cyclic_node.py
64 lines (46 loc) · 1.34 KB
/
first_cyclic_node.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
57
58
59
60
61
62
63
64
"""
Given a linked list, find the first node of a cycle in it.
1 -> 2 -> 3 -> 4 -> 5 -> 1 => 1
A -> B -> C -> D -> E -> C => C
Note: The solution is a direct implementation
Floyd's cycle-finding algorithm (Floyd's Tortoise and Hare).
"""
import unittest
class Node:
def __init__(self, x):
self.val = x
self.next = None
def first_cyclic_node(head):
"""
:type head: Node
:rtype: Node
"""
runner = walker = head
while runner and runner.next:
runner = runner.next.next
walker = walker.next
if runner is walker:
break
if runner is None or runner.next is None:
return None
walker = head
while runner is not walker:
runner, walker = runner.next, walker.next
return runner
class TestSuite(unittest.TestCase):
def test_first_cyclic_node(self):
# create linked list => A -> B -> C -> D -> E -> C
head = Node('A')
head.next = Node('B')
curr = head.next
cyclic_node = Node('C')
curr.next = cyclic_node
curr = curr.next
curr.next = Node('D')
curr = curr.next
curr.next = Node('E')
curr = curr.next
curr.next = cyclic_node
self.assertEqual('C', first_cyclic_node(head).val)
if __name__ == '__main__':
unittest.main()