|All-time Popular Problems|
|Check if subtree|
|Delete Middle of Linked List|
|Given a linked list of 0s, 1s and 2s, sort it.|
|Count nodes of linked list|
|Delete N nodes after M nodes of a linked list|
|Check for Balanced Tree|
|Remove every k'th node|
|k largest elements|
|0 - 1 Knapsack Problem|
|Subarray with given sum|
Given a linked list, the task is to find the n'th node from the end. It is needed to complete a method that takes two argument, head of linked list and an integer n. There are multiple test cases. For each test case, this method will be called individually.
The first line of input contains number of test cases. Every test case cntains two lines. First line of every test case contains two space separated values, number of nodes in linked list followed by value of n. Second line of every test case contains data items of linked list.
Corresponding to each test case, output a single integer that is the nth integer in the linked list from the end. Print -1 if the value of n is greater than the number of elements in the linked list.
1 <= T <= 50
0 <= No of Nodes <= 1000
0 <= Data in Nodes <= 1000
1 2 3 4 5 6 7 8 9
10 -5 -100 5
In the first example, there are 9 nodes in linked list and we need to find 2nd node from end. 2nd node from end os 8. In the second example, there are 4 nodes in linked list and we need to find 5th from end. Since 'n' is more than number of nodes in linked list, output is -1.
Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console, and should not print anything on stdout/console. The task is to complete the function specified, and not to write the full code.