Question: //trying to make a recursive call to function to get a sum of all nodes in a linked list, having problems calling the recursive function
//trying to make a recursive call to function to get a sum of all nodes in a linked list, having problems calling the recursive function sum from main method, explanations appreciated, thanks.
import java.util.*; public class LinkedListSum {class Node { //our constructor for our Nodes int item; Node next; } public int sum (Node x) {int rest=0; if (x==null) { return 0; } else { rest= sum(x.next); } return rest + x.item; }
public static void main(String[] args) { int resultsum= sum() }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
