# Write a function to get Nth node in a Linked List

Write a GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position.

Example:

```Input:  1->10->30->14,  index = 2
Output: 30
The node at index 2 is 30
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

Algorithm:

```1. Initialize count = 0
2. Loop through the link list
a. if count is equal to the passed index then return current
node
b. Increment count
c. change current to point to next of the current.
```

Implementation:

## C

```// C program to find n'th node in linked list
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct Node
{
int data;
struct Node* next;
};

/* Given a reference (pointer to pointer) to the head
of a list and an int, push a new node on the front
of the list. */
void push(struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));

/* put in the data  */
new_node->data  = new_data;

/* link the old list off the new node */

/* move the head to point to the new node */
}

as arguments and return data at index*/
int GetNth(struct Node* head, int index)
{
int count = 0; /* the index of the node we're currently
looking at */
while (current != NULL)
{
if (count == index)
return(current->data);
count++;
current = current->next;
}

/* if we get to this line, the caller was asking
for a non-existent element so we assert fail */
assert(0);
}

/* Drier program to test above function*/
int main()
{

/* Use push() to construct below list
1->12->1->4->1  */

/* Check the count function */
printf("Element at index 3 is %d", GetNth(head, 3));
getchar();
}```

## Java

```// Java program to find n'th node in linked list

class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}

{

/* Takes index as argument and return data at index*/
public int GetNth(int index)
{
int count = 0; /* index of Node we are
currently looking at */
while (current != null)
{
if (count == index)
return current.data;
count++;
current = current.next;
}

/* if we get to this line, the caller was asking
for a non-existent element so we assert fail */
assert(false);
return 0;
}

/* Given a reference to the head of a list and an int,
inserts a new Node on the front of the list. */
public void push(int new_data)
{

/* 1. alloc the Node and put data*/
Node new_Node = new Node(new_data);

/* 2. Make next of new Node as head */

/* 3. Move the head to point to new Node */
}

/* Drier program to test above functions*/
public static void main(String[] args)
{

/* Use push() to construct below list
1->12->1->4->1  */
llist.push(1);
llist.push(4);
llist.push(1);
llist.push(12);
llist.push(1);

/* Check the count function */
System.out.println("Element at index 3 is "+llist.GetNth(3));
}
}
```

## Python

```# A complete working Python program to find n'th node

# Node class
class Node:
# Function to initialise the node object
def __init__(self, data):
self.data = data # Assign data
self.next = None # Initialize next as null

# Linked List class contains a Node object

def __init__(self):

# This function is in LinkedList class. It inserts
# a new node at the beginning of Linked List.
def push(self, new_data):

# 1 & 2: Allocate the Node &
#     Put in the data
new_node = Node(new_data)

# 3. Make next of new Node as head

# 4. Move the head to point to new Node

# Returns data at given index in linked list
def getNth(self, index):
current = self.head # Initialise temp
count = 0 # Index of current node

# Loop while end of linked list is not reached
while (current):
if (count == index):
return current.data
count += 1
current = current.next

# if we get to this line, the caller was asking
# for a non-existent element so we assert fail
assert(false)
return 0;

# Code execution starts here
if __name__=='__main__':

# Use push() to construct below list
# 1->12->1->4->1
llist.push(1);
llist.push(4);
llist.push(1);
llist.push(12);
llist.push(1);

n = 3
print ("Element at index 3 is :", llist.getNth(n))
```

Output:
`Element at index 3 is 4`

Time Complexity: O(n)

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
1.3 Average Difficulty : 1.3/5.0
Based on 217 vote(s)