Node at a given index in linked list | Practice | GeeksforGeeks

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. 
Input:
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.
Output:
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.
Constraints:
1 <= T <= 30
1 <= N <= 100
K <= N
1 <= value <= 103
Example:
Input:
2
6 5
1 2 3 4 657 76
10 2
8 7 10 8 6 1 20 91 21 2
Output:
657
7
Explanation:
Testcase 1: Element at 5th index in the linked list is 657 (1-based indexing).
 
0
uvuv3925710 hours ago
int ans=0;      while(node!=null)      {          ans++;          if(ans==ind)          return node.data;                    node = node.next;      }      return -1;
0
sricharanpindi0051 week ago
def getNth(head, k):   c=0   temp=head   while temp:       c=c+1       if c==k:           return temp.data       temp=temp.next
#python3
0
pk9466171 week ago
I think it is quite easy using recursion ...
int GetNth(struct node* head, int index){ // Code here if(index==1){     return head->data; } if(head!=NULL and index!=1){     return GetNth(head->next,index-1); }} 
0
neelasatyasai931 week ago
#PYTHON CODE
def getNth(head, k):
   # Code here
   c=0
   temp=head
   if temp==None:
       return 0
   else:
       while c != k-1:
           temp=temp.next
           c+=1
       return temp.data
0
saikrishnakasa1232 weeks ago
#here k is the no.of times times the loop should #repeat , so loop until the k and simply print #the data present there
def getNth(head, k):
    c=1
    n=head
    for _ in range(k-1):
        n=n.next
    return n.data
#Thank me ,if you love it!
0
09himanshusah2 weeks ago
#JavaScript Solution
class Solution {
  GetNth(head,index){
    //code here
    let current =  head;
    let count = 1;
    while(current) {
        if(count === index) {
            return current.data;
        }
        current = current.next;
        count++;
    }
  }
}
0
prabhakarsati294262 weeks ago
def getNth(head, k):   # Code here   if (k == 1):       return head.data   temp = head   count = 1   ans = None   while temp is not None:       temp = temp.next       count = count  + 1       if (count == k):           ans = temp.data   return ans
0
riyaraip082 weeks ago
 struct node* temp = head; int c=1; while(temp!=NULL){     if(index==c){         return temp->data;         break;     }     c++;     temp = temp->next; } return 0;
0
erreshusingh2 weeks ago
int GetNth(struct node* head, int index){ // Code here if(index == 1){     return head->data; } else{     return GetNth(head->next, index-1); }}
0
bajpayeevinayak2283 weeks ago
int getback(struct node* cur, int index,int i){ if(i>index-2)   {return cur->data;}
getback(cur->next,index,i+1);}int GetNth(struct node* head, int index){ // Code here struct node *cur=head; int z=getback(cur,index,0); return z;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.