2
LinkedListNode addLists(LinkedListNode l1, LinkedListNode l2, int carry) {

    if(l1 == null && l2 == null && carry == 0) {
      return null;
    }

    LinkedListNode result = new LinkedListNode(carry,null,null);
    int value = carry;
    if(l1 != null) 
      value += l1.data;

    if(l2 != null)
      value += l2.data;

    result.data = value % 10;

    //Recursive call
    if(l1 != null || l2 != null || value >= 10) {
      LinkedListNode more = addLists(l1 == null? null : l1.next,
                                     l2 == null? null : l2.next,
                                     value >= 10? 1 : 0);
      result.setNext(more);
    }
    return result;
}

My doubts with the recursive call of addLists(arg1,arg2,arg3)are:

  1. What exactly is stored at more after each recursive call? In other words, will more stack up the result of each recursive call?
  2. Will the statement (after the recursive call), result.setNext(more), be executed in every recursive call?
  3. How will the final return value work?

I understand the concept of recursion and have gone through various other answers. However, the scenario of return statement along with the recursive call being assigned to moreseem different, and have made it confusing. Would really appreciate a simple explanation of this scenario. Thanks.

merseyside
  • 65
  • 2
  • 6
  • Maybe don't think of it as recursion: think of it as "just calling a method". If the call to `addLists(a, b, c)` were, say, `doSomething(a, b, c)`, would you be able to answer these questions? – Andy Turner Jun 13 '16 at 20:05
  • A recursive method call is no different than a non-recursive one. The called method eventually returns or throws an exception, that's it. In the first case, the execution of the calling method continues normally. – Rogério Jun 13 '16 at 20:06
  • Possible duplicate of [How does the recursion here work?](http://stackoverflow.com/questions/2406824/how-does-the-recursion-here-work) – Prune Jun 13 '16 at 20:50
  • @AndyTurner , please note that I understand the concept of recursion. It is the assigning statement of `LinkedListNode more = addLists(a1,a2,a3)` that is causing the confusion. Will all the results be fetched at once and then assigned to `more`? – merseyside Jun 13 '16 at 21:35
  • @PoojithJain respectfully, I don't think you do understand it. What do you think you mean by "all the results"? There is only 1 result from that method. Just as if it were a method called `doSomething` that returns a single `LinkedListNode`, this is a method called `addLists` which returns a single `LinkedListNode`. That instance *might* have had `setNext` called on it by a recursive call; but that is irrelevant to you, as a caller of the method. – Andy Turner Jun 13 '16 at 21:37
  • @AndyTurner, yes, I do understand that a method would return only a single result. Pardon me for phrasing it in the wrong manner. The statement `result.setNext(more) ` caused the confusion. I thought each result returned by the method would get appended to the node `more`, one at a time. – merseyside Jun 13 '16 at 21:46
  • But this is the point about recursion is "just calling a method": there is no "one at a time" here, there is *just one time*, from the point of view of a single invocation of `addLists`. There is no loop. You execute one method call, you get one result, you call `setNext` once. Forget what is happening within the recursive call; it looks after itself. – Andy Turner Jun 13 '16 at 21:50
  • @AndyTurner, thanks. I thought each result returned by the method would get appended to the node `more`, one at a time, because of `result.setNext(more) `. So, please let me know if I am right in thinking that `result.setNext(more) ` will execute only after all the recursive calls are done. – merseyside Jun 13 '16 at 21:55
  • It will only execute once the call to `addLists` is done. Don't think of it as "after all the recursive calls are done": it is simply after an invocation of *a method the same as any other* completes. – Andy Turner Jun 13 '16 at 22:00

1 Answers1

3

It might feel like magic but it really isn't. In your code you might call methods sequentially like this:

System.out.println("first line");
System.out.println("second line");

but you never question why it prints "second line". It resumes execution when the printing of "first line" is finished.

With recursion it's the same. At one point it hits a base case. In your code its when both the nodes are null and carry is less than 10. Then it finishes by returning and if it was a recursive call the value will be added and this instance of the method will return etc. all the way up to the initial callee which then will continue executing the next line in the same manner as the two System.out.println lines in my example.

Everytime you call a method the variables are different than the previous. So even if there are 10 levels of calls they all have their own l1, l2, carry, and even more and they will all continue with the next line when the current method returns just as any other methiod. Recursive methods are not that special!

Sylwester
  • 47,942
  • 4
  • 47
  • 79
  • Thanks a lot, @Sylwester. I now completely understand how the returning would be done. Just to clarify, please tell me if I am right in thinking that `result.setNext(more)` and `return result` are not executed as part of each recursive call. – merseyside Jun 13 '16 at 22:11
  • @PoojithJain `result.setNext(more)` is not done at the very bottom(last) call, aka base case, but everyone before. The return statement is executed every time. It's how a value is passed from the method to the callee as the bound variable `more` or as final result. – Sylwester Jun 13 '16 at 22:44