private static int recursiveSum(Node head) { //base case if (head.next == null) { return head.value; } //general case return head.value + recursiveSum(head.next); }
private static int recursiveSum(Node head) {
//base case
if (head.next == null) {
return head.value;
}
//general case
return head.value + recursiveSum(head.next);
how do you print the result?
int n = recursiveSum(Linkedlist.head());System.out.println("Total sum = " +n);private static int recursiveSum(Node head) { //base case if (head.next == null) { return head.value; } //general case return head.value + recursiveSum(head.next); }
Please let me know if my code is correct or not :SumofValues(Node head){int sum=0;if(head==null) { return 0;}if(head.next==null) { sum = sum + head.data; return sum;}else{sum = sum + SumofValues(head.next);return sum;}
how do you print the result?
ReplyDeleteint n = recursiveSum(Linkedlist.head());
DeleteSystem.out.println("Total sum = " +n);
private static int recursiveSum(Node head) {
//base case
if (head.next == null) {
return head.value;
}
//general case
return head.value + recursiveSum(head.next);
}
Please let me know if my code is correct or not :
ReplyDeleteSumofValues(Node head)
{
int sum=0;
if(head==null) { return 0;}
if(head.next==null)
{ sum = sum + head.data;
return sum;
}
else
{
sum = sum + SumofValues(head.next);
return sum;
}