Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program for Writing A Function To Get Nth Node In A Linked List
Linked list is a linear data structure where nodes are connected by storing the address of the next node. Finding the nth node means retrieving the value at the nth position in the linked list, which can be accomplished using iterative or recursive methods.
Problem Example
Given linked list: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null Node to find: 3rd position
Output: 3
Explanation: The value at the 3rd position is 3.
Using Iterative Approach
This approach traverses the linked list using a while loop until reaching the desired position or the end of the list.
// Class to create the structure of nodes
class Node {
constructor(data) {
this.value = data;
this.next = null;
}
}
// Function to print the linked list
function print(head) {
let temp = head;
let ans = "";
while (temp.next != null) {
ans += temp.value + " -> ";
temp = temp.next;
}
ans += temp.value + " -> null";
console.log(ans);
}
// Function to add data to linked list
function add(data, head, tail) {
return tail.next = new Node(data);
}
// Function to find the nth node (iterative)
function findNode(head, position) {
let temp = head;
let count = 1;
while (count < position && temp != null) {
temp = temp.next;
count++;
}
return temp;
}
// Creating linked list
let head = new Node(1);
let tail = head;
tail = add(2, head, tail);
tail = add(3, head, tail);
tail = add(4, head, tail);
tail = add(5, head, tail);
tail = add(6, head, tail);
console.log("The given linked list is:");
print(head);
// Finding the 3rd node
let position = 3;
let nthNode = findNode(head, position);
if (nthNode == null) {
console.log(`Position ${position} does not exist in the linked list`);
} else {
console.log(`The value at position ${position} is: ${nthNode.value}`);
}
The given linked list is: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null The value at position 3 is: 3
Using Recursive Approach
This approach uses recursive calls instead of loops to traverse the linked list and find the nth node.
// Class to create the structure of nodes
class Node {
constructor(data) {
this.value = data;
this.next = null;
}
}
// Function to print the linked list
function print(head) {
let temp = head;
let ans = "";
while (temp.next != null) {
ans += temp.value + " -> ";
temp = temp.next;
}
ans += temp.value + " -> null";
console.log(ans);
}
// Function to add data to linked list
function add(data, head, tail) {
return tail.next = new Node(data);
}
// Function to find the nth node (recursive)
function findNodeRecursive(head, position) {
// Base cases
if (head == null) {
return null;
}
if (position == 1) {
return head;
}
// Recursive call
return findNodeRecursive(head.next, position - 1);
}
// Creating linked list
let head = new Node(1);
let tail = head;
tail = add(2, head, tail);
tail = add(3, head, tail);
tail = add(4, head, tail);
tail = add(5, head, tail);
console.log("The given linked list is:");
print(head);
// Finding the 7th node (doesn't exist)
let position = 7;
let nthNode = findNodeRecursive(head, position);
if (nthNode == null) {
console.log(`Position ${position} does not exist in the linked list`);
} else {
console.log(`The value at position ${position} is: ${nthNode.value}`);
}
The given linked list is: 1 -> 2 -> 3 -> 4 -> 5 -> null Position 7 does not exist in the linked list
Comparison
| Approach | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Iterative | O(n) | O(1) | Memory efficiency |
| Recursive | O(n) | O(n) | Cleaner code logic |
Key Points
- Both methods have O(n) time complexity in worst case
- Iterative approach is more space-efficient with O(1) space complexity
- Recursive approach uses O(n) space due to function call stack
- Always check for null nodes to handle edge cases
- Position counting typically starts from 1, not 0
Conclusion
Both iterative and recursive approaches effectively find the nth node in a linked list. The iterative method is preferred for its constant space usage, while the recursive approach offers cleaner, more readable code at the cost of additional memory overhead.
