Give a recursive implementation of sumList. A method signature is given below. p
ID: 3684986 • Letter: G
Question
Give a recursive implementation of sumList. A method signature is given below.
public class IntNode {
private int value;
private IntNode next;
public IntNode(int i) { value = i; }
public int getInt() { return value; }
public void setInt(int v) { value = v; }
public IntNode getNext() { return next; }
public void setNext(IntNode n) { next = n; }
}
public class Sum {
public static void main(String[] args) {
IntNode a = new IntNode(10);
//omitted: code appending more new elements to the list.
int sum = sumList(a);
System.out.println("The sum is " + sum);
}
//TODO: implement recursive sum implementation.
public static int sumList(IntNode head) {
Explanation / Answer
public static int sumList(IntNode head)
{
if(head->Next=NULL)
return head->value;
else
returm head->value+sumList(head->next);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.