Program Code
class Node<T>{
T data;
Node<T> next;
Node(T data){
this.data = data;
}
}
public class Main
{
public static void linklist(Node<Integer> head , int i){
//create a variable count when it will reach ith value it will print data
int count=0;
while(head!=null){
if(count==i){
System.out.print(head.data);
}
count++;
head = head.next;
}
}
public static void main(String[] args) {
//Defining our Linked and data
Node<Integer> a = new Node<>(10);
Node<Integer> b = new Node<>(20);
Node<Integer> c = new Node<>(30);
Node<Integer> d = new Node<>(40);
Node<Integer> e = new Node<>(50);
Node<Integer> f = new Node<>(60);
Node<Integer> g = new Node<>(70);
//Defining the relations
a.next =b;
b.next =c;
c.next =d;
d.next =e;
e.next =f;
f.next =g;
//calling the function to execute the data at the ith position in the linked list
linklist(a,2);
}
}
Output
30
Previous Blog : Length of Linked List / Size of Linked List in Java
0 Comments
if you are not getting it then ask i am glad to help