X

DAYS

:

HOUR

:

MINS

:

SEC

Copied to Clipboard
Find the Sum of Last N nodes of the Linked List
Basic Accuracy: 50.83% Submissions: 26689 Points: 1

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.

 

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Find the Sum of Last N nodes of the Linked List

Output Window