# JavaScript Program for Writing A Function To Get Nth Node In A Linked List

Linked list is a linear data structure and all the nodes are connected to each other by storing the address of the next node. To find the nth node in a linked list means to get the value present at the nth node of the given linked list and that can be done by two methods that are iterative and recursive method.

### Example

Given linked list:
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
Node to find: 3


### Output

3


Explanation: The value present at the third node is 3.

## Iterative Approach

In this approach we will traverse over the linked list directly using the while loop until we reach the last node or the required node.

### Example

// class to create the structure of the nodes
class Node{
constructor(data){
this.value = data;
this.next = null;
}
}

// function to print the linked list
var ans = ""
while(temp.next != null){
ans += temp.value;
ans += " -> "
temp = temp.next
}
ans += temp.value
ans += " -> null"
console.log(ans)
}

return tail.next = new Node(data);
}

// function to find the nth node
while(node > 1 && temp != null){
node--;
temp = temp.next;
}
return temp;
}

console.log("The given linked list is: ")

// defining node to get
var node = 5;

// calling function to get the nth node;
if(nth_node == null){
console.log("The given linked list does not have the Nth Node");
}
else{
console.log("The value present at the nth node is: " + nth_node.value);
}


### Output

The given linked list is:
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> null
The value present at the nth node is: 5


### Time and Space Complexity

The time complexity of the above code is O(N), where N is the size of the given linked list. Here given number may be less compared to the size of the given linked list but in the worst case, we will traverse only up to the length of the linked list.

We are not using any extra space here, which means the time complexity of the above code is O(1).

## Recursive Approach

In this approach, we will traverse over the linked list by using the recursive calls and not the while loop and will implement the previous logic.

### Example

// class to create the structure of the nodes
class Node{
constructor(data){
this.value = data;
this.next = null;
}
}

// function to print the linked list
var ans = ""
while(temp.next != null){
ans += temp.value;
ans += " -> "
temp = temp.next
}
ans += temp.value
ans += " -> null"
console.log(ans)
}

return tail.next = new Node(data);
}

// function to find the nth node
if(node == 1){
}
return null;
}
else{
}
}

console.log("The given linked list is: ")

// defining node to get
var node = 9;

// calling function to get the nth node;
if(nth_node == null){
console.log("The given linked list does not have the " + node + " number of nodes");
}
else{
console.log("The value present at the nth node is: " + nth_node.value);
}


### Output

The given linked list is:
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> null
The given linked list does not have the 9 number of nodes


### Time and Space Complexity

The time and space complexity of the above code is same and that is O(N), where N is the number of nodes in the given linked list. Here space is due to the recursive calls.

## Conclusion

In this tutorial, we have implemented a JavaScript program to find the nth node from the given linked list. If the nth node is not present then we have printed not present otherwise the value present at that node. We have implemented two approaches, one is the iterative using the while loop and another is the recursive approach. Both worked with the time complexity of O(N), but the iterative is better as it takes no extra space.

Updated on: 04-May-2023

32 Views 