-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetching_node2.java
More file actions
48 lines (44 loc) · 851 Bytes
/
fetching_node2.java
File metadata and controls
48 lines (44 loc) · 851 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//fetching the nth node using recursion
class LinkedList
{
static class Node
{
int data;
Node next;
Node(int data)
{
this.data=data; //using this pointer
}
}
static Node insert(Node head, int element)
{
Node n = new Node(element);
n.data=element; //putting teh data inside the node
n.next=head;
head=n;
return head;
}
static int fnode(Node head, int key)
{
int count =1;
if(count == key)
{
return(head.data);
}
return(fnode(head.next, key-1));
}
public static void main(String args[])
{
Node head=null;
head=insert(head, 12);
head=insert(head, 13);
head=insert(head, 14);
head=insert(head, 15);
head=insert(head, 16);
head=insert(head, 17);
head=insert(head, 18);
head=insert(head, 19);
head=insert(head, 20);
System.out.println("Item at index 5 is "+fnode(head, 5));
}
}