1
public void length() {
    System.out.println(length(head, 0));
}
public int length(Node he, int count) {
    if(he!=null) {
        // System.out.println(he.data +"   "+count++);
        // count++;
        // return length(he.next, count);
        return length(he.next, count++);
    }
    return count;
}

In the code above, I have to find the length of linked list. If I run the same code, I am getting the length 0.But, when i use commented code and I am getting the correct length. Why is that happening?

  • 1
    `count++` actually passes `count`, [see this Question how post Increment works](http://stackoverflow.com/questions/2371118/how-do-the-post-increment-i-and-pre-increment-i-operators-work-in-java) – SomeJavaGuy Aug 11 '16 at 07:12
  • What about removing the second parameter and simply `return (he == null) ? 0 : 1 + length(he.next);`? – fabian Aug 11 '16 at 07:20
  • thanks @fabian .. I actually found other way through you. :) – Krishna krish Aug 11 '16 at 09:00

2 Answers2

5

length(he.next, count++) passes the original value of count to the method call, since you are using the post increment operator. Therefore you are always passing 0.

length(he.next, ++count) would work, since here the incremented value of count will be passed.

In your commented code you are not passing the value of count++ to the method call, you are passing count after it was already incremented, which also works.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • but, the code is recursing and for the next recursio step.. it has to increment right? as like waht happens in iterative steps? why didn't that happen in recursion? – Krishna krish Aug 11 '16 at 07:38
  • @Krishnakrish I'm not sure what you mean. You are passing the un-incremented value of `count` to each recursive call. The only reason your recursion ends (instead of producing StackOverflowError) is that the stopping condition depends on the passed `Node` (which advances in each recursive call) and not on the passed `count` (which is always 0). – Eran Aug 11 '16 at 07:43
  • @Eran, looks like he is expecting the value to be incremented after the previous call stack completes, which is not the case. The variables passed as arguments to a recursive method are part of only that particular recursion call stack – JavaHopper Aug 11 '16 at 15:17
0

Use

return length(he.next, ++count);

or

System.out.println(he.data +"   "+count);
count++;
return length(he.next, count);

I will try to simulate your and my code.

Your code :

//count = 0

length(he.next, count++) // length(he.next, 0) 

//after recursion call count increments , count = 1

My code :

//count = 0

// before recursion call, ++count increments count 

// count = 1 

length(he.next, ++count) // length(he.next, 1) 

Guys im new here if im wrong, please edit me :)

burakozgul
  • 775
  • 6
  • 11
  • but, the code is recursing and for the next recursio step.. it has to increment right? as like waht happens in iterative steps? why didn't that happen in recursion? – Krishna krish Aug 11 '16 at 07:38
  • Yes you need to increment. Actually you are incrementing count but incrementation happens after recursion call. Because of that you are passing old value of count which is 0. – burakozgul Aug 11 '16 at 07:44