title: How to sum items in a Node recursively in Java?
body: <p>I'm trying to sum up items in a node both iteratively and recursively. I already wrote the program to do the iterative way and I'm having problems on how this can be done recursively.</p>
  
  <p>code:</p>
  
  <pre><code>public int sumIterative() {
      Node newNode= new Node(item, next);
      int sum = 0;
  
      while(newNode != null){
          sum = sum + item;
          newNode = newNode.next;
      }
      return sum;
  }
  </code></pre>
  
  <p>My effort on making the recursive way:</p>
  
  <pre><code>public int sumRecursive() {
      Node newNode = new Node(item,next);
      int sum = 0;
      int result;
      if(newNode == null){
          result = 0;
      } else {
          return sumRecursive();
      }
      return sum;    
  }
  </code></pre>
  
  <p>I am trying to make this with the logic of "there are no nodes after this"</p>
  
  <p>How is it possible for this to be done recursively?</p>
  
  <p>Edit</p>
  
  <p>This is the public driver method, I can post my whole code on demand</p>
  
  <pre><code>public int sumRecursive() {
      int sum = 0;
      if (top != null) {
          sum = top.sumRecursive();
      }
      return sum;
  }
  
  class LinkedList {
  
      private class Node {
          public int item;
          public Node next;
  
          public Node(int newItem, Node newNext)  {
              item = newItem;
              next = newNext;
          }
  
          public int getItem() {
              return item;
          }
  
          public Node getNext() {
              return next;
          }
  
          public void setNext(Node newNext) {
              next = newNext;
          }
  
          // My Other Node methods
  
          // Recursively add up the numbers stored in this
          // and all the nodes after this.
          // Base case: there are no nodes after this.
          public int sumRecursive() {
              Node node = new Node(item,next);
  
              /*Node newNode = new Node(item,next);
              int sum = 0;
              if (null == newNode) {
                  return sum;
              } else {
                  sum += sumRecursive(newNode.next);
              }*/
  
              if (node == null) {
                  return node.item;
              } else {
                  return node.item + sumRecursive(node.next);
              }
          }
  
          // Iteratively add up the numbers stored in this
          // and all the nodes after this.
          public int sumIterative() {
              Node newNode= new Node(item, next);
              int sum = 0;
  
              while(newNode != null) {
                  sum = sum + item;
                  newNode = newNode.next;
              }
              return sum;
          }
      } // end class Node
  
      private Node top; // a pointer to the first node in the list (when it's not empty)
  
      public LinkedList() {
          top = null; // empty list
      }
  
      // Insert a node at the front of the list
      public void insertAtFront(int newItem)  {
          top = new Node(newItem, top);
      }
  
      // Print out the list (10 numbers per line)
      public void printList() {
          int count = 1;
          Node curr;
  
          if (top == null) {
              System.out.println( "List is empty" );
          } else {
              curr = top;
              while (curr != null) {
                  System.out.print( " " + curr.item );
                  count++;
                  if (count % 10 == 0) {
                      System.out.println();
                  }
                  curr = curr.next;
              }
              if (count % 10 != 0) {
                  System.out.println();
              }
          }
      }
  
      // public driver method for sumRecursive
      public int sumRecursive() {
          int sum = 0;
  
          if (top != null) {
              sum = top.sumRecursive();
          }
  
          return sum;
      }
  
      // public driver method for sumIterative
      public int sumIterative() {
          int sum = 0;
  
          if (top != null) {
              sum = top.sumIterative();
          }
  
          return sum;
      }
  } 
  </code></pre>