Given a single linked list of size M, your task is to complete the function sumOfLastN_Nodes(), which should return the sum of last N nodes of the linked list.
Input:
The function takes two arguments as input, the reference pointer to the head of the linked list and the an integer N.
There will be T test cases and for each test case the function will be called seperately.
Output:
For each test case output the sum of last N nodes of the linked list.
Constraints:
1<=T<=100
1<=N<=M<=1000
Example:
Input:
2
6 3
5 9 6 3 4 10
2 2
1 2
Output:
17
3
Explanation:
Testcase 1: Sum of last three nodes in the linked list is 3 + 4 + 10 = 17.

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3



'''

# Node Class

class Node:

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

        self.data = data

        self.next = None

'''



def sumOfLastN_Nodes(head,n):

    #function should return sum of last n nodes

Solutions:
def sumOfLastN_Nodes(head, n):
	if n == 0 or head is None:
		return 0
	slow = head
	fast = head
	for _ in range(n):
		if fast is None:
			return 0
		fast = fast.next
	while fast:
		slow = slow.next
		fast = fast.next
	node_sum = 0
	while slow:
		node_sum += slow.data
		slow = slow.next
	return node_sum


def sumOfLastN_Nodes(head, n):
	dummy = head
	total = 0
	while dummy:
		total += 1
		dummy = dummy.next
	curr_node = 0
	dummy = head
	nodes_count = total - n
	node_sum = 0
	while dummy:
		if curr_node >= nodes_count:
			node_sum += dummy.data
		curr_node += 1
		dummy = dummy.next
	return node_sum


def sumOfLastN_Nodes(head, n):
	length = 0
	dummy = head
	while dummy:
		length += 1
		dummy = dummy.next
	k = length - n
	dummy = head
	while k > 0:
		dummy = dummy.next
		k -= 1
	sum = 0
	while dummy:
		sum += dummy.data
		dummy = dummy.next
	return sum


def sumOfLastN_Nodes(head, n):
	c = []
	while head:
		c.append(head.data)
		head = head.next
	k = len(c)
	l = k - n
	return sum(c[l:k])


def sumOfLastN_Nodes(head, n):
	res = 0
	i = 0
	prev = None
	curr = head
	while curr:
		new_node = curr.next
		curr.next = prev
		prev = curr
		curr = new_node
	head = prev
	a = head
	while i < n:
		res += a.data
		a = a.next
		i += 1
	return res


def sumOfLastN_Nodes(head, n):
	length = 0
	sum = 0
	count = 0
	temp = head
	prev = None
	while temp:
		nexxt = temp.next
		temp.next = prev
		prev = temp
		temp = nexxt
	head = prev
	for i in range(n):
		sum = sum + head.data
		head = head.next
	return sum


def sumOfLastN_Nodes(head, n):
	count = 0
	lis = []
	while head:
		count += 1
		lis.append(head.data)
		head = head.next
	tot = len(lis) - n
	return sum(lis[tot:])


def sumOfLastN_Nodes(head, n):
	curr = head
	c = 0
	while curr:
		curr = curr.next
		c += 1
	sum = 0
	curr = head
	for i in range(c):
		if i >= c - n:
			sum += curr.data
		curr = curr.next
	return sum


def sumOfLastN_Nodes(head, n):
	temp = head
	for x in range(n - 1):
		temp = temp.next
	fr = head
	while temp.next:
		temp = temp.next
		fr = fr.next
	sum = 0
	while fr:
		sum += fr.data
		fr = fr.next
	return sum


def sumOfLastN_Nodes(head, n):
	lst_len = 0
	result_sum = 0
	counter = 1
	next_node = head
	while next_node:
		lst_len += 1
		next_node = next_node.next
	x = lst_len - n
	next_node = head
	while next_node:
		if counter > x:
			result_sum = result_sum + next_node.data
		next_node = next_node.next
		counter += 1
	return result_sum


def sumOfLastN_Nodes(head, n):
	z = None
	while head:
		x = head.next
		head.next = z
		z = head
		head = x
	res = 0
	while z:
		res = res + z.data
		n = n - 1
		if n == 0:
			return res
		z = z.next


def sumOfLastN_Nodes(head, n):
	temp = head
	num = []
	while temp:
		num.append(temp.data)
		temp = temp.next
	return sum(num[-n:])


def sumOfLastN_Nodes(head, n):
	temp = head
	stack = []
	while temp:
		stack.append(temp.data)
		temp = temp.next
	sum = 0
	while n:
		sum += stack.pop()
		n = n - 1
	return sum


def sumOfLastN_Nodes(head, n):
	prev = None
	cur = head
	sum = 0
	while cur:
		temp = cur.next
		cur.next = prev
		prev = cur
		cur = temp
	while n:
		sum += prev.data
		prev = prev.next
		n -= 1
	return sum


def sumOfLastN_Nodes(head, n):
	temp = head
	c = 0
	while temp:
		temp = temp.next
		c = c + 1
	temp = head
	for i in range(c - n):
		temp = temp.next
	sum = 0
	while temp:
		sum = sum + temp.data
		temp = temp.next
	return sum


def sumOfLastN_Nodes(head, n):
	lent = 0
	cur = head
	while cur:
		lent += 1
		cur = cur.next
	x = lent - n
	temp = head
	for i in range(1, x + 1):
		temp = temp.next
	res = temp
	s = 0
	while res != None:
		s += res.data
		res = res.next
	return s


def sumOfLastN_Nodes(head, n):
	lis = []
	while head:
		lis.append(head.data)
		head = head.next
	lis.reverse()
	return sum(lis[:n:1])


def sumOfLastN_Nodes(head, n):
	sum1 = 0
	l = 0
	curr = head
	while curr != None:
		sum1 += curr.data
		l += 1
		curr = curr.next
	curr = head
	for i in range(l - n):
		sum1 -= curr.data
		curr = curr.next
	return sum1


def sumOfLastN_Nodes(head, n):
	h = head
	for _ in range(n):
		h = h.next
	while h:
		h = h.next
		head = head.next
	summa = 0
	while head:
		summa += head.data
		head = head.next
	return summa


def sumOfLastN_Nodes(head, n):
	k = head
	l = []
	p = 0
	sm = 0
	while k is not None:
		p += 1
		k = k.next
	k = head
	while p:
		p -= 1
		if p < n:
			l.append(k.data)
		k = k.next
	s = sum(l)
	return s


def sumOfLastN_Nodes(head, n):
	ll = []
	curr = head
	while curr:
		ll.append(curr.data)
		curr = curr.next
	ll.reverse()
	total = sum(ll[:n])
	return total


def sumOfLastN_Nodes(head, n):
	k = 0
	curr = head
	m = 0
	sum = 0
	while curr != None:
		m += 1
		sum += curr.data
		curr = curr.next
	curr = head
	while k != m - n:
		sum -= curr.data
		curr = curr.next
		k += 1
	return sum


def sumOfLastN_Nodes(head, n):
	a = []
	while head != None:
		a.append(head.data)
		head = head.next
	b = a[::-1]
	return sum(b[0:n])


def sumOfLastN_Nodes(head, n):
	l = []
	while head != None:
		l.append(head.data)
		head = head.next
	l.reverse()
	return sum(l[:n:1])


def sumOfLastN_Nodes(head, n):
	count = 0
	ptr = head
	sum = 0
	while ptr:
		count += 1
		ptr = ptr.next
	N_start = count - n
	i = 0
	ptr2 = head
	while i < N_start:
		ptr2 = ptr2.next
		i += 1
	for j in range(N_start, count):
		sum = sum + ptr2.data
		ptr2 = ptr2.next
	return sum


def sumOfLastN_Nodes(head, n):
	count = 0
	result = 0
	current = head
	while current:
		count += 1
		current = current.next
	current = head
	while count - n:
		current = current.next
		count -= 1
	while current:
		result += current.data
		current = current.next
	return result


def sumOfLastN_Nodes(head, n):
	c = 0
	t = head
	while t != None:
		c = c + 1
		t = t.next
	d = c - n
	t = head
	sum = 0
	while t != None:
		if d == 0:
			break
		t = t.next
		d = d - 1
	while t != None:
		sum = sum + t.data
		t = t.next
	return sum


def sumOfLastN_Nodes(head, n):
	ptr = head
	l = []
	while ptr is not None:
		l.append(ptr.data)
		ptr = ptr.next
	l.reverse()
	sumi = 0
	for i in range(0, n):
		sumi += l[i]
	return sumi


def sumOfLastN_Nodes(head, n):
	temp = head
	size = 0
	sum = 0
	while temp != None:
		size = size + 1
		sum = sum + temp.data
		temp = temp.next
	temp = head
	i = 1
	while temp != None and i <= size - n:
		sum = sum - temp.data
		i = i + 1
		temp = temp.next
	return sum


def sumOfLastN_Nodes(head, n):
	if head == None:
		return 0
	counter = 0
	node = head
	while node:
		counter += 1
		node = node.next
	pos = counter - n
	node1 = head
	counter2 = 0
	while counter2 != pos:
		counter2 += 1
		node1 = node1.next
	sum1 = 0
	while node1:
		sum1 += node1.data
		node1 = node1.next
	return sum1


def sumOfLastN_Nodes(head, n):
	curr = head
	x = []
	while curr:
		x.append(curr.data)
		curr = curr.next
	b = x[-1::-1]
	c = b[0:n]
	return sum(c)


def sumOfLastN_Nodes(head, n):
	slow = head
	fast = head
	while fast and n:
		fast = fast.next
		n -= 1
	s = 0
	while fast:
		fast = fast.next
		slow = slow.next
	while slow:
		s += slow.data
		slow = slow.next
	return s


def sumOfLastN_Nodes(head, n):
	temp = head
	length = 0
	s = 0
	while temp:
		s = s + temp.data
		length = length + 1
		temp = temp.next
	temp = head
	for _ in range(length - n):
		s = s - temp.data
		temp = temp.next
	return s


def sumOfLastN_Nodes(head, n):
	counter = 0
	copy = head
	while copy != None:
		counter = counter + 1
		copy = copy.next
	m = counter - n
	sum = 0
	while head != None:
		if m == 0:
			sum = sum + head.data
		else:
			m = m - 1
		head = head.next
	return sum


def sumOfLastN_Nodes(head, n):
	c = 0
	t = head
	while t != None:
		c += 1
		t = t.next
	r = head
	k = 0
	p = 0
	while r != None:
		p = p + 1
		if p > c - n:
			k = k + r.data
		r = r.next
	return k


def sumOfLastN_Nodes(head, n):
	c = 0
	temp = head
	sum1 = 0
	while temp:
		c += 1
		temp = temp.next
	temp = head
	t = c + 1
	c = 0
	while temp:
		c += 1
		if c >= t - n:
			sum1 += temp.data
		temp = temp.next
	return sum1


def sumOfLastN_Nodes(head, n):
	slow = head
	fast = head
	for i in range(n):
		fast = fast.next
	while fast:
		slow = slow.next
		fast = fast.next
	sum = 0
	while slow:
		sum = sum + slow.data
		slow = slow.next
	return sum


def sumOfLastN_Nodes(head, n):
	index = currentIndex = 1
	tempHead = head
	total = 0
	while head:
		if currentIndex - index >= n:
			index += 1
			tempHead = tempHead.next
		currentIndex += 1
		head = head.next
	while tempHead:
		total += tempHead.data
		tempHead = tempHead.next
	return total


def sumOfLastN_Nodes(head, n):
	st = []
	res = 0
	count = 0
	i = 0
	j = 0
	ptr = head
	while ptr != None:
		count += 1
		st.append(ptr.data)
		ptr = ptr.next
	for i in range(count - n, count):
		res += st[i]
	return res


def sumOfLastN_Nodes(head, n):
	m = 0
	current_node = head
	while current_node is not None:
		m = m + 1
		current_node = current_node.next
	s = 0
	current_node = head
	for i in range(1, m - n + 1):
		current_node = current_node.next
	for j in range(m - n + 1, m + 1):
		s = s + current_node.data
		current_node = current_node.next
	return s


def sumOfLastN_Nodes(head, n):
	l = []
	p = head
	while p:
		l.append(p.data)
		p = p.next
	L = len(l)
	k = L - n
	d = l[k:L]
	return sum(d)


def sumOfLastN_Nodes(head, n):
	curr = head
	lst = []
	sum = 0
	while curr:
		lst.append(curr.data)
		curr = curr.next
	while n > 0:
		sum += lst[len(lst) - n]
		n -= 1
	return sum


def sumOfLastN_Nodes(head, n):
	c = 0
	temp = head
	while temp:
		c += 1
		temp = temp.next
	k = c - n
	temp2 = head
	for i in range(k):
		temp2 = temp2.next
	ctr = 0
	while temp2:
		ctr += temp2.data
		temp2 = temp2.next
	return ctr


def sumOfLastN_Nodes(head, n):
	h = head
	(c, s) = (0, 0)
	while h:
		s += h.data
		c += 1
		h = h.next
	h = head
	for i in range(c - n):
		s -= h.data
		h = h.next
	return s


def sumOfLastN_Nodes(head, n):
	temp = []
	pre = head
	while pre:
		temp.append(pre)
		pre = pre.next
	count = 0
	for i in range(len(temp) - n, len(temp)):
		count += temp[i].data
	return count


def sumOfLastN_Nodes(head, n):
	if not head:
		return False
	(prev, curr) = (head, head)
	while n:
		n -= 1
		curr = curr.next
	while curr:
		prev = prev.next
		curr = curr.next
	sum = 0
	while prev:
		sum += prev.data
		prev = prev.next
	return sum


def sumOfLastN_Nodes(head, n):
	temp = head
	size = 0
	while temp:
		size += 1
		temp = temp.next
	x = size - n + 1
	count = sum = 0
	t = head
	while t:
		count += 1
		if count >= x:
			sum += t.data
		t = t.next
	return sum


def sumOfLastN_Nodes(head, n):
	l = []
	while head:
		l.append(head.data)
		head = head.next
	m = sum(l[-n:])
	return m


def sumOfLastN_Nodes(head, n):
	curr = head
	length = 0
	while curr is not None:
		length += 1
		curr = curr.next
	if n > length:
		return 0
	curr = head
	for i in range(length - n):
		curr = curr.next
	total = 0
	while curr is not None:
		total += curr.data
		curr = curr.next
	return total


def sumOfLastN_Nodes(head, n):
	c = 0
	cur1 = head
	while cur1:
		c += 1
		cur1 = cur1.next
	i = 1
	sum = 0
	cur = head
	while cur:
		if i > c - n:
			sum += cur.data
			cur = cur.next
			i += 1
		else:
			cur = cur.next
			i += 1
	return sum


def sumOfLastN_Nodes(head, N):
	arr = []
	while head:
		arr.append(head.data)
		head = head.next
	arr = arr[::-1]
	return sum(arr[:N])


def sumOfLastN_Nodes(head, n):
	c = 0
	p = head
	while p != None:
		c = c + 1
		p = p.next
	for i in range(c - n):
		head = head.next
	sum = 0
	while head:
		sum = sum + head.data
		head = head.next
	return sum


def sumOfLastN_Nodes(head, n):
	n1 = head
	li = []
	sum = 0
	while n1 is not None:
		li.append(n1.data)
		n1 = n1.next
	i = -1
	while i >= -n:
		sum = sum + li[i]
		i = i - 1
	return sum


def sumOfLastN_Nodes(head, n):
	node = head
	h = 0
	while node is not None:
		node = node.next
		h += 1
	fn = h - n
	l = 0
	sum = 0
	while head is not None:
		l += 1
		if l > fn and l <= h:
			sum += head.data
		head = head.next
	return sum


def sumOfLastN_Nodes(head, n):
	current = head
	temp = head
	l = []
	while current:
		l.append(current.data)
		current = current.next
	if len(l) == n:
		return sum(l)
	else:
		return sum(l[len(l) - n:])


def sumOfLastN_Nodes(node, n):
	values = []
	while node:
		values.append(node.data)
		node = node.next
	return sum(values[-n:])


def sumOfLastN_Nodes(head, n):
	k = 0
	Tsum = 0
	Curr = head
	while Curr:
		k += 1
		Tsum += Curr.data
		Curr = Curr.next
	sum = 0
	P = head
	c = 0
	while c != k - n:
		sum += P.data
		P = P.next
		c += 1
	return Tsum - sum


def sumOfLastN_Nodes(head, n):
	curr = head
	count = 0
	while curr:
		curr = curr.next
		count += 1
	curr = head
	i = 0
	while i != count - n:
		i += 1
		head = head.next
	sum = 0
	while head:
		sum += head.data
		head = head.next
	return sum


def sumOfLastN_Nodes(head, n):
	temp = head
	for i in range(n):
		temp = temp.next
	while temp:
		temp = temp.next
		head = head.next
	s = 0
	while head:
		s += head.data
		head = head.next
	return s


def sumOfLastN_Nodes(head, n):
	start = head
	temp = head
	c = 0
	i = 0
	while i < n:
		i += 1
		c += temp.data
		temp = temp.next
	while temp != None:
		c = c + temp.data - start.data
		start = start.next
		temp = temp.next
	return c


def sumOfLastN_Nodes(head, n):
	temp = head
	c = 0
	while temp:
		c += 1
		temp = temp.next
	temp = head
	a = c - n
	for i in range(a):
		temp = temp.next
	s = 0
	while temp:
		s += temp.data
		temp = temp.next
	return s


def sumOfLastN_Nodes(head, n):
	l = []
	temp = head
	while temp:
		l.append(temp.data)
		temp = temp.next
	sum = 0
	if n == len(l):
		for i in range(0, len(l)):
			sum = sum + l[i]
	else:
		for i in range(len(l) - n, len(l)):
			sum = sum + l[i]
	return sum


def sumOfLastN_Nodes(head, n):
	first = head
	for i in range(n):
		first = first.next
	while first != None:
		first = first.next
		head = head.next
	sum = 0
	while head != None:
		sum += head.data
		head = head.next
	return sum


def sumOfLastN_Nodes(head, n):
	temp = None
	temp = head
	count = 0
	count1 = 0
	c = 0
	temp1 = head
	while temp is not None:
		temp = temp.next
		count += 1
	while temp1 is not None:
		if count - n > count1:
			temp1 = temp1.next
			count1 += 1
		else:
			c += temp1.data
			temp1 = temp1.next
	return c


def sumOfLastN_Nodes(head, n):
	l = []
	while head:
		l.append(head.data)
		head = head.next
	r = l[::-1]
	b = r[:n]
	return sum(b)


def sumOfLastN_Nodes(head, n):
	temp = head
	count = 1
	while temp is not None:
		count += 1
		temp = temp.next
	count = count - n
	temp = head
	data = 0
	while temp is not None:
		if count != 0:
			count -= 1
		if count == 0:
			data += temp.data
		temp = temp.next
	return data


def sumOfLastN_Nodes(head, n):
	if not head:
		return 0
	first = head
	for i in range(n):
		if not first:
			return 0
		first = first.next
	second = head
	while first:
		first = first.next
		second = second.next
	total = 0
	while second:
		total += second.data
		second = second.next
	return total


def sumOfLastN_Nodes(head, n):
	sum = 0
	if head == None:
		return None
	N = 0
	tmp_node = head
	while tmp_node:
		N += 1
		tmp_node = tmp_node.next
	exclude = N - n
	if exclude <= 0:
		exclude = 0
	tmp_node = head
	while exclude:
		tmp_node = tmp_node.next
		exclude -= 1
	while tmp_node:
		sum += tmp_node.data
		tmp_node = tmp_node.next
	return sum


def sumOfLastN_Nodes(head, n):
	t = head
	l = 0
	while t:
		t = t.next
		l += 1
	if l < n:
		return
	t = head
	for _ in range(l - n):
		t = t.next
	s = 0
	while t:
		s += t.data
		t = t.next
	return s


def sumOfLastN_Nodes(head, n):
	cur = head
	l = []
	while cur:
		l.append(cur.data)
		cur = cur.next
	l.reverse()
	sum = 0
	for a in range(n):
		sum += l[a]
	return sum


def sumOfLastN_Nodes(head, n):
	c = 0
	t = 0
	tem = head
	while head:
		head = head.next
		c += 1
	c -= n
	res = 0
	while tem:
		if t >= c:
			res += tem.data
		tem = tem.next
		t += 1
	return res


def sumOfLastN_Nodes(head, n):
	leng = 0
	temp = head
	while temp != None:
		leng += 1
		temp = temp.next
	count = leng - n
	temp = head
	count1 = 0
	sum1 = 0
	if count == 0:
		while temp != None:
			sum1 = sum1 + temp.data
			temp = temp.next
	else:
		while temp != None:
			count1 += 1
			temp = temp.next
			if count1 == count:
				break
		while temp != None:
			sum1 = sum1 + temp.data
			temp = temp.next
	return sum1


def reverseList(head):
	curr = head
	prev = None
	next = None
	while curr:
		next = curr.next
		curr.next = prev
		prev = curr
		curr = next
	return prev

def sumOfLastN_Nodes(head, n):
	nhead = reverseList(head)
	sum = 0
	aux = nhead
	for i in range(n):
		sum += aux.data
		aux = aux.next
	return sum


def sumOfLastN_Nodes(head, n):
	l = []
	i = 0
	while head:
		l.append(head.data)
		head = head.next
	l.reverse()
	s = sum(l[0:n])
	return s


def sumOfLastN_Nodes(head, n):
	stack = []
	temp = head
	sum = 0
	while temp is not None:
		stack.append(temp.data)
		temp = temp.next
	while n != 0:
		sum += stack.pop()
		n = n - 1
	return sum


def sumOfLastN_Nodes(head, n):
	t = head
	s = 0
	c = 0
	while t != None:
		c += 1
		t = t.next
	l = c - n
	c = 0
	t = head
	while t != None:
		c += 1
		if c > l:
			s += t.data
		t = t.next
	return s


def sumOfLastN_Nodes(head, n):
	c = 0
	t = head
	while head:
		c += 1
		head = head.next
	r = c - n
	y = 0
	i = 0
	while t:
		y += 1
		if y > r:
			i = i + t.data
		t = t.next
	return i


def sumOfLastN_Nodes(head, n):
	s = []
	temp = head
	while temp != None:
		s.append(temp.data)
		temp = temp.next
	return sum(s[-n:])


def sumOfLastN_Nodes(head, n):
	c = 0
	ref = head
	sm = 0
	while head:
		c += 1
		sm += head.data
		head = head.next
		if c > n:
			sm -= ref.data
			ref = ref.next
	return sm


def sumOfLastN_Nodes(head, n):
	node = head
	count = 0
	while node:
		count += 1
		node = node.next
	node = head
	sum = 0
	while count:
		if count == n:
			sum += node.data
			n -= 1
		node = node.next
		count -= 1
	return sum


def sumOfLastN_Nodes(head, n):
	count = 0
	temp = head
	while head:
		count += 1
		head = head.next
	count1 = 0
	head = temp
	ans = 0
	while head:
		if count1 + n >= count:
			ans += head.data
		count1 += 1
		head = head.next
	return ans


def length_list(head):
	count = 0
	while head:
		count += 1
		head = head.next
	return count

def sumOfLastN_Nodes(head, n):
	sum = 0
	length = length_list(head)
	if n > length:
		return -1
	curr = head
	for i in range(length - n):
		curr = curr.next
	while curr:
		sum += curr.data
		curr = curr.next
	return sum


def sumOfLastN_Nodes(head, n):
	temp = head
	l = []
	while temp:
		l.append(temp.data)
		temp = temp.next
	l = l[::-1]
	p = sum(l[:n])
	return p


def sumOfLastN_Nodes(head, n):
	cur = head
	v = []
	while cur is not None:
		v.append(cur.data)
		cur = cur.next
	sum = 0
	for i in range(len(v) - n, len(v)):
		sum += v[i]
	return sum