Write a function to get Nth node in a Linked List
Last Updated :
10 Jan, 2023
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
Algorithm:
1. Initialize count = 0
2. Loop through the link list
a. if the count is equal to the passed index then return the current
node
b. Increment count
c. change current to point to next of the current.
Implementation:
C++
#include <assert.h>
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* next;
};
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int GetNth(Node* head, int index)
{
Node* current = head;
int count = 0;
while (current != NULL) {
if (count == index)
return (current->data);
count++;
current = current->next;
}
assert (0);
}
int main()
{
Node* head = NULL;
push(&head, 1);
push(&head, 4);
push(&head, 1);
push(&head, 12);
push(&head, 1);
cout << "Element at index 3 is " << GetNth(head, 3);
return 0;
}
|
C
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node
= ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int GetNth( struct Node* head, int index)
{
struct Node* current = head;
int count = 0;
while (current != NULL) {
if (count == index)
return (current->data);
count++;
current = current->next;
}
assert (0);
}
int main()
{
struct Node* head = NULL;
push(&head, 1);
push(&head, 4);
push(&head, 1);
push(&head, 12);
push(&head, 1);
printf ( "Element at index 3 is %d" , GetNth(head, 3));
getchar ();
}
|
Java
import java.io.*;
class Node {
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
class LinkedList {
Node head;
public int GetNth( int index)
{
Node current = head;
int count = 0 ;
while (current != null )
{
if (count == index)
return current.data;
count++;
current = current.next;
}
assert ( false );
return 0 ;
}
public void push( int new_data)
{
Node new_Node = new Node(new_data);
new_Node.next = head;
head = new_Node;
}
public static void main(String[] args)
{
LinkedList llist = new LinkedList();
llist.push( 1 );
llist.push( 4 );
llist.push( 1 );
llist.push( 12 );
llist.push( 1 );
System.out.println( "Element at index 3 is "
+ llist.GetNth( 3 ));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def push( self , new_data):
new_node = Node(new_data)
new_node. next = self .head
self .head = new_node
def getNth( self , index):
current = self .head
count = 0
while (current):
if (count = = index):
return current.data
count + = 1
current = current. next
assert (false)
return 0
if __name__ = = '__main__' :
llist = LinkedList()
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))
|
C#
using System;
using System.Diagnostics;
public class Node {
public int data;
public Node next;
public Node( int d)
{
data = d;
next = null ;
}
}
class LinkedList {
Node head;
public int GetNth( int index)
{
Node current = head;
int count = 0;
while (current != null ) {
if (count == index)
return current.data;
count++;
current = current.next;
}
Debug.Assert( false );
return 0;
}
public void push( int new_data)
{
Node new_Node = new Node(new_data);
new_Node.next = head;
head = new_Node;
}
public static void Main(String[] args)
{
LinkedList llist = new LinkedList();
llist.push(1);
llist.push(4);
llist.push(1);
llist.push(12);
llist.push(1);
Console.WriteLine( "Element at index 3 is "
+ llist.GetNth(3));
}
}
|
Javascript
<script>
class Node {
constructor(d) {
this .data = d;
this .next = null ;
}
}
var head;
function GetNth(index)
{
var current = head;
var count = 0;
while (current != null ) {
if (count == index)
return current.data;
count++;
current = current.next;
}
assert ( false );
return 0;
}
function push(new_data) {
var new_Node = new Node(new_data);
new_Node.next = head;
head = new_Node;
}
push(1);
push(4);
push(1);
push(12);
push(1);
document.write( "Element at index 3 is "
+ GetNth(3));
</script>
|
Output
Element at index 3 is 4
Time Complexity: O(n)
Auxiliary Space: O(1) space created for variables
Method 2- With Recursion
This method is contributed by MY_DOOM.
Algorithm:
Algorithm
getnth(node,n)
1. Initialize count = 0
2. if count==n
return node->data
3. else
return getnth(node->next,n-1)
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node
= ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int GetNth( struct Node* head, int n)
{
if (head == NULL)
return -1;
if (n == 0)
return head->data;
return GetNth(head->next, n - 1);
}
int main()
{
struct Node* head = NULL;
push(&head, 1);
push(&head, 4);
push(&head, 1);
push(&head, 12);
push(&head, 1);
printf ( "Element at index 3 is %d" , GetNth(head, 3));
getchar ();
}
|
C
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
void push(Node** head_ref, int new_data)
{
Node* new_node = (Node*) malloc ( sizeof (Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int GetNth(Node* head, int n)
{
if (head == NULL)
return -1;
if (n == 0)
return head->data;
return GetNth(head->next, n - 1);
}
int main()
{
struct Node* head = NULL;
push(&head, 1);
push(&head, 4);
push(&head, 1);
push(&head, 12);
push(&head, 1);
printf ( "Element at index 3 is %d" , GetNth(head, 3));
getchar ();
}
|
Java
import java.io.*;
class GFG {
static class Node {
int data;
Node next;
Node( int data) { this .data = data; }
}
static Node push(Node head, int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
return head;
}
static int GetNth(Node head, int n)
{
int count = 0 ;
if (head == null )
return - 1 ;
if (count == n)
return head.data;
return GetNth(head.next, n - 1 );
}
public static void main(String args[])
{
Node head = null ;
head = push(head, 1 );
head = push(head, 4 );
head = push(head, 1 );
head = push(head, 12 );
head = push(head, 1 );
System.out.printf( "Element at index 3 is %d" ,
GetNth(head, 3 ));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def push( self , new_data):
new_node = Node(new_data)
new_node. next = self .head
self .head = new_node
def getNth( self , llist, position):
llist.getNthNode( self .head, position, llist)
def getNthNode( self , head, position, llist):
count = 0
if (head):
if count = = position:
print (head.data)
else :
llist.getNthNode(head. next , position - 1 , llist)
else :
print ( 'Index Doesn\'t exist' )
if __name__ = = "__main__" :
llist = LinkedList()
llist.push( 1 )
llist.push( 4 )
llist.push( 1 )
llist.push( 12 )
llist.push( 1 )
print ( "Element at Index 3 is" , end = " " )
llist.getNth(llist, 3 )
|
C#
using System;
class GFG {
public class Node {
public int data;
public Node next;
public Node( int data) { this .data = data; }
}
static Node push(Node head, int new_data)
{
Node new_node = new Node(new_data);
new_node.data = new_data;
new_node.next = head;
head = new_node;
return head;
}
static int GetNth(Node head, int n)
{
if (head == null )
return -1;
int count = 0;
if (count == n)
return head.data;
return GetNth(head.next, n - 1);
}
public static void Main()
{
Node head = null ;
head = push(head, 1);
head = push(head, 4);
head = push(head, 1);
head = push(head, 12);
head = push(head, 1);
Console.Write( "Element at index 3 is {0}" ,
GetNth(head, 3));
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function push(head , new_data) {
var new_node = new Node(new_data);
new_node.data = new_data;
new_node.next = head;
head = new_node;
return head;
}
function GetNth(head , n) {
var count = 0;
if (head == null )
return -1;
if (count == n)
return head.data;
return GetNth(head.next, n - 1);
}
var head = null ;
head = push(head, 1);
head = push(head, 4);
head = push(head, 1);
head = push(head, 12);
head = push(head, 1);
document.write( "Element at index 3 is " , GetNth(head, 3));
</script>
|
Output
Element at index 3 is 4
Time Complexity : O(n)
Auxiliary Space: O(n) due to recursive calls.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...