• + 3 comments

    I did not understand the logic. Can you please explain? Thanks!

    • + 36 comments

      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.

      • + 0 comments

        Got it. Thanks!

      • + 2 comments

        Youre amazing thank you

        • + 0 comments

          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.)

        • + 1 comment

          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); } }

          • + 0 comments

            Really helpfull!!

      • + 0 comments

        Many thanks.. this helped tremendously

        :D

      • + 0 comments

        Very educational. Thank you!

      • + 0 comments

        Hi ,Could you give more details about 2nd paragraph .How it's >[RevPr1(RevPr2(RevPr3()))]?

      • + 0 comments

        I've been seeing recursion a lot and only vaguely understand it, but with your explanation, it's all clear now. Thank you!

      • + 1 comment

        Thank you!!!

      • + 1 comment

        Great explanation

      • + 1 comment

        Seriously amazing, should be a teacher or a prof.

      • + 1 comment

        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.

        • + 1 comment

          that made it a little more clearer . thanks :)

      • + 0 comments

        Your explanation was very helpful..

      • + 1 comment

        That was a beauty. I believe you cleared the concepts of all recursive functions for me.

      • + 0 comments

        massssss...

      • + 0 comments

        UR FUCKIN AWEOSME MAN

      • + 0 comments

        beautiful explanation. One of the clearest I've seen on recursion honestly lol

      • + 0 comments

        awesome explained recursion . it help for beginner ! keep it.

      • + 0 comments

        thank you so much!

      • + 0 comments

        Thank you so much

      • + 0 comments

        thanks a lot bro..

      • + 0 comments

        Thanks man for the explanation.

      • + 0 comments

        it really helps!

      • + 0 comments

        Thoroughly explained!

      • + 0 comments

        I read many explanation but i didnt get it. After reading your explanation i got it. Thanks

      • + 0 comments

        Wow.. Great explanation

      • + 0 comments

        May god bless you my friend, You are amazing!

      • + 0 comments

        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!

      • + 0 comments

        Thank u so much bro now i can understand the code

      • + 0 comments

        this explanation is amazing. thanks a lot

      • + 0 comments

        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

      • + 0 comments

        bro you are lit.!

      • + 0 comments

        Thanks a lot well explained .

      • + 1 comment

        Thank you very much for explaining to me. Could you give some resources so that i can understand recursion well

        • + 0 comments

          Start writing the normal, fibnocci, prime numbers, even/odd and so on programs using recursion, it will help to improve your understanding.

      • + 0 comments

        Thank you, this helped me understand recursive more :)

      • + 0 comments

        thanks

    • + 0 comments

      fantastic!

    • + 1 comment

      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); } }

      • [deleted]
        + 0 comments

        best logic!!!