-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodular_node.py
45 lines (35 loc) · 971 Bytes
/
modular_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
import math
class Node:
def __init__(self, data):
self.data = data
self.next = None
def add(data):
node = Node(data)
node.next = None
return node
def find_modular_node(head, k):
if (k < 0 or head == None):
return None;
current = head;
modular_node = None
while (current):
if (current.data % k == 0):
modular_node = current;
current = current.next;
return modular_node;
head = add(1)
head.next = add(2)
head.next.next = add(3)
head.next.next.next = add(4)
head.next.next.next.next = add(5)
head.next.next.next.next.next = add(6)
head.next.next.next.next.next.next = add(7)
head.next.next.next.next.next.next.next = add(8)
head.next.next.next.next.next.next.next.next = add(9)
head.next.next.next.next.next.next.next.next.next = add(10)
k = 4
modular_node = find_modular_node(head, k);
if (modular_node != None):
print (modular_node.data)
else:
print ("N/A")