java - summing list in recursion -
whats problem? i've tried sum list , returns 0
public static int sum(list<integer> l, node<integer> pos, int sum) { if(pos==null) homecoming 0; sum=sum+pos.getinfo(); pos=pos.getnext(); homecoming sum+sum(l, pos, sum); } public static void main(string[] args) { int sum=0; list<integer> l = new list<integer>(); node<integer> pos = l.getfirst(); l = input(l, pos); system.out.println(l); system.out.println(sum(l, pos, sum)); } thanks.
you in comments phone call site follows:
public static void main(string[] args) { int sum = 0; list<integer> l = new list<integer>(); node<integer> pos = l.getfirst(); //<======= `pos' refers empty list l = input(l, pos); system.out.println(l); system.out.println(sum(l, pos, sum)); } the problem that, since initialize pos before add together list, sum original (empty) list.
java list recursion
No comments:
Post a Comment