I want the recursive function to check the value in the current node equals the value in the next node and increment by 1 else don’t increment and move on until the end of the list. So for, a list of 1, 2, 2, 3 , 1, 1. It should return 2 as 2, 2 is one adjacent duplicate and 1, 1 is another adjacent duplicate.
I can’t figure out how to handle the false case when the value of the current isn’t equal to the value of the next. Basically, not incrementing.
This is my code so far…
int fn(Node l) {
if (l == null)
return 0;
else
return (l.value == l.next.value) ? (1 + fn(l.next)) : ;
}
The function needs to be called again in either case, for the false case you would not add
1to the return value, i.e.You should also check that the
l.nextis notnullfirst. So you could rewrite the function…