Given a singly linked list and a number k, you are required to complete the function modularNode() which returns the modular node of the linked list.
A modular node is the last node of the linked list whose Index is divisible by the number k, i.e. i%k==0.
Note: If no such node is available, return -1. We are following 1 indexing.
Example 1:
Input: LinkedList: 1->2->3->4->5->6->7
       k = 3
Output: 6
Explanation: Indexing of the linked list is
same as the values of the Node. The maximum
index is which is divisble by 3 is 6 and the
node value of the 6th index is 6.
Example 2:
Input: LinkedList: 19->28->37->46->55
           k = 2
Output: 46
Explanation: Index start from 1 and end at 5.
The maximum Index which is divisble by 2 is 4
and 4th Node in the list is 46.
 
Your Task:
You don't need to read input or print anything. Complete the function modularNode() which takes the head Node and integer k as input parameters and returns the modular Node, if exists, -1 otherwise.
Constraints:
1 <= T <= 100
1 <= N <= 500

Source: geeksforgeeks | Difficulty: EASY
Tags: Number theory

Data structures

Starter code: #User function Template for python3



'''

class Node:

    def __init__(self, data):   # data -> value stored in node

        self.data = data

        self.next = None

'''

def modularNode(head, k):

    #function should return the modular Node

    #if no such node is present then return -1



Solutions:
def modularNode(head, k):
	temp = head
	if k < 1 or head is None:
		return -1
	c = []
	while head:
		c.append(head.data)
		head = head.next
	m = len(c)
	s = m % k
	j = m - s - 1
	if k > m:
		return -1
	return c[j]


def modularNode(head, k):
	max_mode = set()
	temp = head
	count = 0
	is_modular_node_found = False
	while temp:
		count += 1
		if count % k == 0:
			max_mode.add(count)
			is_modular_node_found = True
		temp = temp.next
	if not is_modular_node_found:
		return -1
	max_ind = max(max_mode)
	count = 0
	temp = head
	while temp:
		count += 1
		if count == max_ind:
			return temp.data
		temp = temp.next


def modularNode(head, k):
	temp = head
	num = [-1]
	i = 1
	while temp:
		if i % k == 0:
			num.append(temp.data)
		temp = temp.next
		i += 1
	return num[-1]


def modularNode(head, k):
	a = []
	while head != None:
		a.append(head.data)
		head = head.next
	b = len(a) % k
	c = len(a) - b
	if len(a) >= k:
		return a[c - 1]
	else:
		return -1


def modularNode(head, k):
	temp = head
	n = 0
	p = 0
	l = []
	while temp is not None:
		l.append(temp.data)
		temp = temp.next
		p += 1
	l.reverse()
	n = p // 2
	if p == 1:
		return l[0]
	for i in range(n):
		if p % k == 0:
			count = True
			return l[i]
		p -= 1
	return -1


def modularNode(head, k):
	current = head
	idx = 0
	divisible_by_k = {}
	key = 0
	while current:
		idx += 1
		if not idx % k:
			divisible_by_k[idx] = current.data
			key = idx
		current = current.next
	return divisible_by_k[key] if divisible_by_k else -1


def modularNode(head, k):
	i = 1
	mod = -1
	while head != None:
		if i % k == 0:
			mod = head.data
		head = head.next
		i += 1
	return mod


def modularNode(head, k):
	c = 1
	l = []
	curr = head
	while curr != None:
		if c % k == 0:
			l.append(curr.data)
		curr = curr.next
		c += 1
	if len(l) == 0:
		return -1
	return l[-1]


def modularNode(head, k):
	l = []
	curr = head
	i = 1
	while curr != None:
		if i % k == 0:
			l.append(curr.data)
		i += 1
		curr = curr.next
	if len(l) != 0:
		return l[-1]
	else:
		return -1


def modularNode(head, k):
	i = 1
	res = Node(-1)
	while head:
		if i % k == 0:
			res = head
		head = head.next
		i += 1
	return res.data


def modularNode(head, k):
	ptr = head
	ansi = 0
	l = []
	while ptr is not None:
		l.append(ptr.data)
		ptr = ptr.next
	if len(l) == 1:
		return 1
	else:
		for i in range(len(l) - 1, 0, -1):
			if (i + 1) % k == 0:
				ansi = l[i]
				break
			else:
				ansi = -1
		return ansi


def modularNode(head, k):
	out = -1
	count = 0
	curr = head
	while curr:
		count += 1
		curr = curr.next
	x = count // k
	a = x * k
	temp = 1
	curr = head
	while curr:
		if temp == a:
			out = curr.data
		temp += 1
		curr = curr.next
	return out


class Node:

	def __init__(self, data):
		self.data = data
		self.next = None

def modularNode(head, k):
	i = 1
	res = Node(-1)
	while head:
		if i % k == 0:
			res = head
		head = head.next
		i += 1
	return res.data


def modularNode(head, k):
	temp = head
	length = 0
	a = -1
	temp1 = head
	while temp:
		length = length + 1
		if length % k == 0:
			a = temp.data
		temp = temp.next
	return a


def modularNode(head, k):
	count = 1
	node = None
	while head != None:
		if count % k == 0:
			node = head
		count += 1
		head = head.next
	if node != None:
		return node.data
	else:
		return -1


def modularNode(head, k):
	res = 0
	c = 0
	temp = head
	while temp != None:
		c += 1
		if c % k == 0:
			res = temp.data
		temp = temp.next
	if res == 0:
		return -1
	return res


def modularNode(head, k):
	ind = -1
	temp = head
	c = 0
	while temp is not None:
		c += 1
		if c % k == 0:
			ind = temp.data
		temp = temp.next
	return ind


def modularNode(head, k):
	index = 0
	modular_node = None
	while head:
		index += 1
		if not index % k:
			modular_node = head
		head = head.next
	if modular_node is None:
		return -1
	return modular_node.data


def modularNode(head, k):
	a = -1
	i = 1
	current_node = head
	while current_node is not None:
		if i % k == 0:
			a = current_node.data
		current_node = current_node.next
		i = i + 1
	return a


def modularNode(head, k):
	curr = head
	isModular = False
	count = 0
	while curr is not None:
		count += 1
		if count % k == 0:
			mod = curr.data
			isModular = True
		curr = curr.next
	if not isModular:
		return -1
	else:
		return mod


def modularNode(head, k):
	rt = head
	val = -1
	c = 0
	while rt:
		c += 1
		if c % k == 0:
			val = rt.data
		rt = rt.next
	return val


def modularNode(head, k):
	if not head:
		return False
	count = 1
	ans = -1
	while head:
		if count % k is 0:
			ans = head.data
		count += 1
		head = head.next
	return ans


def modularNode(head, k):
	c = 0
	temp = head
	while temp:
		c += 1
		temp = temp.next
	p = c % k
	n = c - p
	if n > 0:
		temp2 = head
		for i in range(1, n):
			temp2 = temp2.next
		return temp2.data
	else:
		return -1


def modularNode(head, k):
	h = head
	s = 0
	c = 1
	co = 0
	while h:
		if c % k == 0:
			if co < c:
				co = c
				s = h.data
		h = h.next
		c += 1
	if s == 0:
		return -1
	else:
		return s


def modularNode(head, k):
	temp = head
	g = 0
	t = -1
	while temp:
		g += 1
		if g % k == 0:
			t = temp.data
		temp = temp.next
	return t


def modularNode(head, k):
	c = 0
	cur = head
	while cur:
		c += 1
		cur = cur.next
	x = 0
	for i in range(c, 0, -1):
		if i % k == 0:
			x = i
			break
	else:
		return -1
	j = 1
	cur2 = head
	while cur2:
		if j == x:
			return cur2.data
		else:
			cur2 = cur2.next
			j += 1


def modularNode(head, k):
	var = head
	index = 1
	sol = 0
	while var:
		if index % k == 0:
			sol = var.data
		var = var.next
		index += 1
	if sol == 0:
		return -1
	else:
		return sol


def modularNode(head, k):
	if head == None:
		return -1
	temp = head
	max = -1
	count = 1
	while temp != None:
		if count % k == 0:
			max = temp.data
		temp = temp.next
		count += 1
	return max


def modularNode(node, k):
	item = -1
	index = 1
	while node:
		if index % k == 0:
			item = node.data
		node = node.next
		index += 1
	return item


def modularNode(node, k):
	nodes = []
	while node:
		nodes.append(node.data)
		node = node.next
	index = len(nodes) - len(nodes) % k - 1
	if index < 0:
		return -1
	else:
		return nodes[index]


def modularNode(head, k):
	c = 0
	temp = head
	while temp:
		c += 1
		temp = temp.next
	if c < k:
		return -1
	else:
		a = c // k * k
		temp = head
		while a > 1:
			temp = temp.next
			a -= 1
		return temp.data


def modularNode(head, k):
	i = 1
	temp = head
	modular = None
	while temp:
		if i % k == 0:
			modular = temp
		temp = temp.next
		i += 1
	return modular.data if modular else -1


def modularNode(head, k):
	pos = 1
	cur = head
	sem = -1
	while cur:
		if pos % k == 0:
			sem = cur.data
		cur = cur.next
		pos += 1
	return sem


def modularNode(head, k):
	temp = head
	max = -1
	c = 1
	while temp:
		if c % k == 0:
			max = temp.data
		c += 1
		temp = temp.next
	return max


def modularNode(head, k):
	temp = head
	count = 0
	lst = []
	while temp is not None:
		count += 1
		if count % k == 0:
			lst.append(temp.data)
		temp = temp.next
	if not lst:
		return -1
	else:
		return lst[-1]


def modularNode(head, k):
	count = 1
	temp = head
	a = -1
	while temp is not None:
		if count % k == 0:
			a = temp.data
		count += 1
		temp = temp.next
	return a


def modularNode(head, k):
	if not head:
		return -1
	length = 0
	node = head
	while node:
		length += 1
		node = node.next
	if length < k:
		return -1
	index = length - length % k
	node = head
	for i in range(1, index):
		node = node.next
	return node.data if node else -1


def modularNode(head, k):
	if not head:
		return -1
	modular = None
	current = head
	i = 1
	while current:
		if i % k == 0:
			modular = current
		current = current.next
		i += 1
	return modular.data if modular else -1


def modularNode(head, k):
	tmp = head
	node = None
	i = 1
	while tmp:
		if i % k == 0:
			node = tmp
		tmp = tmp.next
		i += 1
	return node.data if node != None else -1


def modularNode(head, k):
	cur = head
	count = 1
	last_modular = -1
	while cur:
		if count % k == 0:
			last_modular = cur.data
		cur = cur.next
		count += 1
	return last_modular


def modularNode(head, k):
	cur = head
	l = []
	while cur:
		l.append(cur.data)
		cur = cur.next
	r = k * (len(l) // k)
	if r == 0:
		return -1
	else:
		return l[r - 1]


def modularNode(head, k):
	if k <= 0 or head == None:
		return None
	i = 1
	modularNode = None
	temp = head
	while temp != None:
		if i % k == 0:
			modularNode = temp
		i = i + 1
		temp = temp.next
	if i < k:
		return -1
	return modularNode.data


def modularNode(head, k):
	c = 1
	node = None
	while head:
		if c % k == 0:
			node = head.data
		c += 1
		head = head.next
	if node == None:
		return -1
	return node


def modularNode(head, k):
	aux = head
	nodo = None
	index = 1
	while aux:
		if index % k == 0:
			nodo = aux
		aux = aux.next
		index += 1
	if nodo:
		return nodo.data
	else:
		return -1


def modularNode(head, k):
	max = 0
	i = 1
	while head:
		if i % k == 0:
			if i >= max:
				max = i
				mod_node = head.data
			else:
				pass
		i = i + 1
		head = head.next
	if max:
		return mod_node
	else:
		return -1


def modularNode(head, k):
	ans = -1
	count = 0
	while head:
		count += 1
		if count % k == 0:
			ans = head.data
		head = head.next
	return ans


def modularNode(head, k):
	ans = -100
	s = head
	c = 0
	i = 1
	while s != None:
		if i % k == 0:
			ans = s.data
			c = 1
		s = s.next
		i += 1
	return ans if c else -1


def modularNode(head, k):
	x = 1
	result = -1
	while head != None:
		if x % k == 0:
			result = head.data
		x += 1
		head = head.next
	return result


def modularNode(head, k):
	ans = -1
	temp = head
	index = 1
	while temp != None:
		if index % k == 0:
			ans = temp.data
		temp = temp.next
		index += 1
	return ans


def modularNode(head, k):
	l = []
	h = head
	while h.next != None:
		l.append(h.data)
		h = h.next
	l.append(h.data)
	for i in range(len(l) - 1, -1, -1):
		if (i + 1) % k == 0:
			return l[i]
	return -1


def modularNode(head, k):
	curr = head
	i = 1
	maxi = 0
	while curr:
		if i % k == 0:
			maxi = curr.data
			curr = curr.next
			i += 1
		else:
			i += 1
			curr = curr.next
	return maxi if maxi > 0 else -1


def modularNode(head, k):
	stack = []
	temp = head
	count = 1
	if temp == None:
		return -1
	while temp != None:
		if count % k == 0:
			stack.append(temp.data)
		temp = temp.next
		count += 1
	if stack:
		return stack.pop()
	else:
		return -1


def modularNode(head, k):
	r = -1
	temp = head
	i = 0
	while temp:
		i += 1
		if i % k == 0:
			r = temp.data
		temp = temp.next
	return r


def modularNode(head, k):
	current_node = head
	count = 0
	while current_node is not None:
		count += 1
		current_node = current_node.next
	copycount = count
	current_node = head
	modular = None
	while count > 0:
		i = copycount - count + 1
		if i % k == 0:
			modular = current_node
		current_node = current_node.next
		count -= 1
	if modular is None:
		return -1
	return modular.data


def modularNode(head, k):
	b = []
	tmp = head
	i = 1
	x = -1
	while tmp != None:
		b.append(tmp.data)
		if i % k == 0:
			x = i
		tmp = tmp.next
		i += 1
	if x == -1:
		return x
	return b[x - 1]


class Node:

	def __init__(self, data):
		self.data = data
		self.next = None

def modularNode(head, k):
	itr = head
	count = 1
	res = 0
	while itr:
		if count % k == 0:
			res = itr.data
		count = count + 1
		itr = itr.next
	if res != 0:
		return res
	else:
		return -1


def modularNode(head, k):
	mod = -1
	temp = head
	j = 1
	while temp:
		if j % k == 0:
			mod = temp.data
			j = 1
		else:
			j += 1
		temp = temp.next
	return mod


def modularNode(head, k):
	c = 0
	f = head
	while head != None:
		c += 1
		head = head.next
	if k <= c:
		d = c - c % k
		while d - 1:
			f = f.next
			d -= 1
		return f.data
	return -1


def modularNode(head, k):
	l = -1
	i = 1
	while head:
		if i % k == 0:
			l = head.data
		i += 1
		head = head.next
	return l


def modularNode(head, k):
	l = []
	l1 = []
	i = 0
	temp1 = head
	temp = head
	while temp != None:
		i += 1
		l.append(i)
		l1.append(temp.data)
		temp.data = i
		temp = temp.next
	m = []
	for j in l:
		if j % k == 0:
			m.append(j)
	if len(m) == 0:
		return -1
	else:
		m1 = sorted(m)
		a = m[-1]
		return l1[a - 1]


def modularNode(head, k):
	count = 0
	temp = head
	while temp is not None:
		count += 1
		temp = temp.next
	a = count % k
	b = count - a
	t = head
	c = 0
	while t is not None:
		c += 1
		if c == b:
			break
		t = t.next
	if t is None:
		return -1
	else:
		return t.data


def modularNode(head, k):
	modularNode = None
	ix = 1
	while head:
		if ix % k == 0:
			modularNode = head
		head = head.next
		ix += 1
	if not modularNode:
		return -1
	else:
		return modularNode.data


def modularNode(head, k):
	node = head
	i = 0
	while node:
		node = node.next
		i += 1
	while i % k != 0 and i > 1:
		i -= 1
	node = head
	ii = i
	while ii > 1:
		node = node.next
		ii -= 1
	if i != 1:
		return node.data
	else:
		return -1


def modularNode(head, k):
	n = 1
	result = -1
	while head:
		if n % k == 0:
			result = head.data
		head = head.next
		n += 1
	return result


def modularNode(head, k):
	(start, st, c) = (head, [], 1)
	while start:
		if c % k == 0:
			st.append(start.data)
		c += 1
		start = start.next
	if not st:
		return -1
	return st.pop()


def modularNode(head, k):
	i = 1
	p = head
	ele = -1
	while p != None:
		if i % k == 0:
			ele = p.data
		p = p.next
		i += 1
	return ele


def modularNode(head, k):
	p = head
	c = 1
	z = -1
	while p != None:
		if c % k == 0:
			z = p.data
		p = p.next
		c += 1
	return z


def modularNode(head, k):
	temp = head
	l = 0
	while head != None:
		l += 1
		head = head.next
	if k > l:
		return -1
	while l > 0:
		if l % k == 0:
			for i in range(1, l):
				temp = temp.next
			return temp.data
		else:
			l -= 1
	return -1


def modularNode(head, k):
	temp = head
	cnt = 0
	ans = -1
	while temp:
		cnt += 1
		if cnt % k == 0:
			ans = temp.data
		temp = temp.next
	return ans


def modularNode(head, k):
	index = 0
	temp = -500
	while head:
		index += 1
		if index % k == 0:
			temp = head.data
		head = head.next
	if temp == -500:
		return -1
	else:
		return temp


def modularNode(head, k):
	modNode = 0
	temp = head
	index = 1
	while head is not None:
		if index % k == 0:
			modNode = head.data
		head = head.next
		index += 1
	if not modNode:
		return -1
	else:
		return modNode


def modularNode(head, k):
	curr = head
	c = 0
	while curr:
		curr = curr.next
		c += 1
	x = c // k * k
	if x == 0:
		return -1
	temp = head
	i = 1
	while temp:
		if i == x:
			return temp.data
		temp = temp.next
		i += 1


def modularNode(head, k):
	x = -1
	y = 1
	cur = head
	while cur:
		if y % k == 0:
			x = cur.data
		else:
			pass
		cur = cur.next
		y += 1
	return x


def modularNode(head, k):
	l = []
	a = head
	n = 0
	while a:
		a = a.next
		n += 1
		if n % k == 0:
			l.append(n)
	a = head
	if len(l) > 0:
		for i in range(1, l[-1]):
			a = a.next
		return a.data
	else:
		return -1


def modularNode(head, k):
	n = head
	i = 1
	modular_node = -1
	while n != None:
		if i % k == 0:
			modular_node = n.data
		n = n.next
		i += 1
	return modular_node


def modularNode(head, k):
	temp = head
	num = -1
	idx = 1
	while temp:
		if idx % k == 0:
			num = temp.data
		temp = temp.next
		idx = idx + 1
	return num


def modularNode(head, k):
	itr = head
	mod_node = -1
	c = 1
	while itr:
		if c % k == 0:
			mod_node = itr.data
		itr = itr.next
		c += 1
	return mod_node