We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I took sophist_pt's code and altered it a bit to hopefully make it more intuitive.
void ReversePrint(Node head) {
//if the head is not yet null...
if (head != null) {
//call the function again but on the next node
ReversePrint(head.next);
/*----------------------------------------------
The recursion will get "stacked" right here!
The code below this area will not get called
until we go back up through the recursive stack.
-----------------------------------------------*/
System.out.println(head.data);
}
}
Here's a code trace for an example.
We have a linked list of three elements [1] -> [2] -> [3] Let's begin by calling the head.
A. RevPr(1)
if elemenent 1 is not yet null, move on.
It's not null! Call again on next, element 2...
B. RevPr(2)
if elemenent 2 is not yet null, move on.
It's not null! Call again on next, element
3...
C. RevPr(3)
if elemenent 3 is not yet null,
move on. It's not null! Call again
on next, element 4...
D. RevPr(4)
if elemenent 4 is not yet null, move on.
Wait, it is null! No more calls, we're done.
So we just built this recursive stack.
[RevPr1(RevPr2(RevPr3(RevPr4)))]
Now we have to execute the function that is innermost. Why? Because that's where we were most recently!
D. RevPr4. That most recent call failed the
condition. Nothing is printed.
-->[RevPr1(RevPr2(RevPr3()))]
C. RevPr3. RevPr3 never finished the code in
the if statement, so now we print 3!
-->3
-->[RevPr1(RevPr2())]
B. RevPr2. Now we need to print 2!
-->3
-->2
-->[RevPr1()]
A. RevPr1. The last call! Print 1! This call is done, and
now everything is done.
-->3
-->2
-->1
This function was written from sophist_pt's brilliant code.
My pleasure! Recursion hurts my brain too... But eventually with enough practice one is able to understand when and how recursion is useful. (Hopefully without spending a half-hour to trace the process out like I did.)
void ReversePrint(Node *head)
{
// This is a "method-only" submission.
// You only need to complete this method.
if(head){
ReversePrint(head->next);
printf("%d\n",head->data);
}
}
This comment will probably get burried, but basically...if i understood this correctly, the last one who calls and enters the if (head != null) get's to do the print statement first.
Then it unravells until the first one who called the if statements get's their turn to print it out.
Excellent explanation! sophist_pt's answer (and the other recursion answers) confused me. From reading yours I understood it and realized I need some more work on recursion! Thanks!
does recursion allow as to move one step backward
i dont no why you say that we just built this recursive stack. [RevPr1(RevPr2(RevPr3(RevPr4)))]
in which line does it mean that
and if it is a stsck so we moves backward why does it read the only last code (print (head.data))
i am sorry if i didnot explain my problem in easy way so you can understand me as i am a beginner
thanks for your explanation
void ReversePrint(Node *head)
{
// This is a "method-only" submission.
// You only need to complete this method.
if(head){
ReversePrint(head->next);
printf("%d\n",head->data);
}
}
Print in Reverse
You are viewing a single comment's thread. Return to all comments →
I did not understand the logic. Can you please explain? Thanks!
I took sophist_pt's code and altered it a bit to hopefully make it more intuitive.
Here's a code trace for an example. We have a linked list of three elements [1] -> [2] -> [3] Let's begin by calling the head.
So we just built this recursive stack. [RevPr1(RevPr2(RevPr3(RevPr4)))] Now we have to execute the function that is innermost. Why? Because that's where we were most recently!
This function was written from sophist_pt's brilliant code.
Got it. Thanks!
Youre amazing thank you
My pleasure! Recursion hurts my brain too... But eventually with enough practice one is able to understand when and how recursion is useful. (Hopefully without spending a half-hour to trace the process out like I did.)
void ReversePrint(Node *head) { // This is a "method-only" submission. // You only need to complete this method. if(head){ ReversePrint(head->next); printf("%d\n",head->data); } }
Really helpfull!!
Many thanks.. this helped tremendously
:D
Very educational. Thank you!
Hi ,Could you give more details about 2nd paragraph .How it's >[RevPr1(RevPr2(RevPr3()))]?
I've been seeing recursion a lot and only vaguely understand it, but with your explanation, it's all clear now. Thank you!
Thank you!!!
Great explanation
Seriously amazing, should be a teacher or a prof.
This comment will probably get burried, but basically...if i understood this correctly, the last one who calls and enters the if (head != null) get's to do the print statement first.
Then it unravells until the first one who called the if statements get's their turn to print it out.
that made it a little more clearer . thanks :)
Your explanation was very helpful..
That was a beauty. I believe you cleared the concepts of all recursive functions for me.
massssss...
UR FUCKIN AWEOSME MAN
beautiful explanation. One of the clearest I've seen on recursion honestly lol
awesome explained recursion . it help for beginner ! keep it.
thank you so much!
Thank you so much
thanks a lot bro..
Thanks man for the explanation.
it really helps!
Thoroughly explained!
I read many explanation but i didnt get it. After reading your explanation i got it. Thanks
Wow.. Great explanation
May god bless you my friend, You are amazing!
Excellent explanation! sophist_pt's answer (and the other recursion answers) confused me. From reading yours I understood it and realized I need some more work on recursion! Thanks!
Thank u so much bro now i can understand the code
this explanation is amazing. thanks a lot
does recursion allow as to move one step backward i dont no why you say that we just built this recursive stack. [RevPr1(RevPr2(RevPr3(RevPr4)))] in which line does it mean that and if it is a stsck so we moves backward why does it read the only last code (print (head.data)) i am sorry if i didnot explain my problem in easy way so you can understand me as i am a beginner thanks for your explanation
bro you are lit.!
Thanks a lot well explained .
Thank you very much for explaining to me. Could you give some resources so that i can understand recursion well
Start writing the normal, fibnocci, prime numbers, even/odd and so on programs using recursion, it will help to improve your understanding.
Thank you, this helped me understand recursive more :)
thanks
fantastic!
void ReversePrint(Node *head) { // This is a "method-only" submission. // You only need to complete this method. if(head){ ReversePrint(head->next); printf("%d\n",head->data); } }
best logic!!!