/*
Following is the Node class already written for the Linked List
class Node<T> {
T data;
Node<T> next;
public Node(T data) {
this.data = data;
}
}
*/
public class Solution {
public static void printReverse(Node<Integer> root) {
if(root==null) return;
printReverse(root.next);
System.out.print(root.data+" ");
}
}
this is recursive call
jisme first in last out hoga
lets suppose 1 2 3 4 ye ek linkedList hai
ab ham printRever(root.next) se function ko recursive call kar rhe
jisme sabse phle 1 hoga phir check karega root next hai ? nhi hai to dubara recursive
call hoga 2 ko check karega ye v nhi hai
3 ko check karega ye v nhi hai null
4 ko check karega ye v nhi hai null
ab jaise hi 4 ho jayega ye oppositve direction me print krna suru kr dega
4 to 3 to 2 to 1 aur is tarh se ye reverse ho jayega
Comments
Post a Comment