-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProblem-2.java
47 lines (33 loc) · 879 Bytes
/
Problem-2.java
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
/**
* @author Akash Goyal
*/
/**
--------------------- Problem----------->> Nth node from end of linked list
Problem Link :-https://practice.geeksforgeeks.org/problems/nth-node-from-end-of-linked-list/1
*/
class GfG
{
//Function to find the data of nth node from the end of a linked list.
int getNthFromLast(Node head, int n)
{
// Your code here
//Make a window;
Node start=head;
Node end=head;
while(n>0 && end!=null)
{
end=end.next;
n--;
}
if(n>0 && end==null)
{
return -1;
}
while(end!=null)
{
start=start.next;
end=end.next;
}
return start.data;
}
}