count elements in linked list recursively!
Good morning,
I was asked to implement a method which I can count elements in linked list recursively.I tried and it compiles but whenever I run I found stackoverflow error.
here is my trial
public int countRec(){
return countRec1(head);
}
public int countRec1(Link current){
current = head ;
if ( current == null ){
return 0 ;
}
else{
return 1 +countRec1(current.next);
}
any help please? :(
Re: count elements in linked list recursively!
Re: count elements in linked list recursively!
That means you have some infinite recursion happening. Verify your base case, and also that your recursive call is in fact traversing the list and not just looping over the same item forever.