Given a linked list consisting of **L** nodes and given a number **N**. The task is to find the **N**^{th} node from the end of the linked list.

**Input:**

First line of input contains number of testcase T. For each testcase, first line of input contains number of nodes in the linked list and the number N. Next line contains N nodes of linked list.

**Output:**

For each testcase, output the data of node which is at N^{th} distance from end.

**User Task:**

The task is to complete the function **getNthFromLast**() which takes two **arguments**: **reference **to **head and N** and you need to** return N ^{th} **from end.

**Constraints:**

1 <= T <= 200

1 <= L <= 10^{3}

1 <= N <= 10^{3}

**Example:**

**Input:**

2

9 2

1 2 3 4 5 6 7 8 9

4 5

10 5 100 5

**Output:**

8

-1

**Explanation:
Testcase 1:** 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.

Author: AdityaChatterjee

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

the_coder95 | 465 |

thanuvinu94 | 388 |

adityasuman2025 | 305 |

Senbonzakura_Kageyoshi | 297 |

PrakharBansal1 | 274 |

the_coder95 | 1691 |

RishabhTanwar1 | 1110 |

thanuvinu94 | 1064 |

samay_kam_hai | 724 |

themanhasnoname | 620 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4897 |

Quandray | 4547 |

Login to report an issue on this page.