I am trying to count the number of items in a linked list that fit a condition (if x > num) using recursion and iterator. When I use return to self-call the function (as shown below) it returns a count of the items it goes through but doesn't go through the list but when I don't use return and would just call the function directly if(x > num) total = 1 + nodes.Greater(); else nodes.Greater(); it goes through the entire list but returns a count of 0 always.
public static Integer nodesGreater(Node x, Integer num) {
Integer total = 0;
for(Iterator<Integer> it = list.iterator(); it.hasNext();) {
Integer x = it.next();
if(x > num) return 1 + nodesGreater(it.next(), num);
else return nodesGreater(it.next(), num);
}
return total;
}
For example, if the linked list is
1-3-8-10-12-15-20
and the condition is it should be greater than 8, it would return 2 (10, 12) instead of 4 (10, 12, 15, 20)
itvariables)