Node at a given index in linked list
Submissions: 39382   Accuracy:


  Difficulty: Basic   Marks: 1
Associated Course(s):   Interview Preparation

Given a singly linked list with N nodes and a number X. The task is to find the node at the given index (X)(1 based index) of linked list. 

First line of input contains number of testcases T. For each testcase, first line of input contains space seperated two integers, length of linked list and X.

For each testcase, there will be single line of output containing data at Xth node.

User Task:
The task is to complete the function GetNth() which takes head reference and index as arguments and should return the data at Xth position in the linked list.

1 <= T <= 30
1 <= N <= 100
K <= N
1 <= value <= 103


6 5
1 2 3 4 657 76
10 2
8 7 10 8 6 1 20 91 21 2


Testcase 1:
Element at 5th index in the linked list is 657 (1-based indexing).


** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Harshit Sidhwa
Author: atharv

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.

Need help with your code? Please use, generate link and share the link here.

to report an issue on this page.