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?

3
  • 1
    count++ actually passes count, see this Question how post Increment works Commented Aug 11, 2016 at 7:12
  • What about removing the second parameter and simply return (he == null) ? 0 : 1 + length(he.next);? Commented Aug 11, 2016 at 7:20
  • thanks @fabian .. I actually found other way through you. :) Commented Aug 11, 2016 at 9:00

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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?
@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, 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
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 :)

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.