Associated Course(s):
Interview Preparation

Given a linked list of length **L**. The task is to find the **N**^{th} node from the end of the linked list. 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.

**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 N. Next line contains nodes of linked list.

**Output:**

For each testcase, output the data of node which is at Nth distance from end.

**User Task:**

The task is to complete the function **getNthFromLast**() which two arguments reference to head and N for which Nth from end is to be returned.

**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 1

**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

PrateekTiwari1 | 182 |

blackshadows | 154 |

TusharSharma3 | 151 |

dhananjaykajla | 134 |

udrasht | 124 |

Amit Kushwaha 1 | 551 |

TusharSharma3 | 496 |

mb1973 | 396 |

dhananjaykajla | 360 |

SaurabhPatil3 | 359 |

akhayrutdinov | 4973 |

Ibrahim Nash | 4712 |

Quandray | 4289 |

sanjay05 | 3668 |

GB11 | 2857 |

Login to report an issue on this page.