title: Linked Lists Java Replacing Nodes
body: <p>I'm having difficulty with a linked lists program. I want to write a method that destructively replaces the value in each node n by the sum of the values in the tail of the list. So if the list is 2,3,5,7; I want to change it to 17,15,12,7. I am given a program where I have to add a method that does this. I can change the first number, but I can not change the other three, and I am stuck. If anyone could help me, that would be great. </p>
  
  <p>Original Program</p>
  
  <pre><code>public class IntList {
  private int value;     
  private IntList next;
  
  
  public IntList(int v, IntList n) {          // Constructor
      value = v;
      next = n;
    }
  
  public int getValue() { return value; }       // Getters
  public IntList getNext() { return next; }
  public void setValue(int v) { value = v; }    // Setters
  public void setNext(IntList n) { next = n; }
  
  // Find the last node of a linked list.
  public IntList findLast() {
     if (getNext() == null) return this;
     else return getNext().findLast();
   }
  
  // Add a new node with value v at the end of l;
  
  public void addEnd(int v) {
      findLast().setNext(new IntList(v,null));
    }
  
  // Add up the values in a list, recurring down the owner
  public int sumList() {
     if (getNext() == null) return getValue();
     else return getValue() + getNext().sumList();
    }
  
  
  // Convert list of int to string
  
  // Recursive method for constructing the end of the string, after the
  // initial open bracket.
  
   public String toString1() {
     if (getNext() == null)
        return getValue() + "]";
     else return getValue() + ", " + getNext().toString1();
   }
  
  // Top level rountine that starts with the "[" and then calls toString1 to
  // do the rest.
  
    public String toString() {
      return "[" + toString1();
    }
  
  // Recursive method for finding the sum of a list, using recursion down
  // an argument. Note that this is a static method, associated with the class
  // not with an object owner.
  
   static public int sumListArg(IntList l) {
      if (l==null) return 0;
      else return l.getValue() + sumListArg(l.getNext());
     }
  
   static public void main(String[] args) {
     IntList l = new IntList(2,null);
     l.addEnd(3);
     l.addEnd(5);
     l.addEnd(7);
     System.out.println("h");
     System.out.println(l.toString());
     System.out.println("Sum = " + l.sumList());
  } // end main
  } // end RecursiveIntList    
  </code></pre>
  
  <p>Here is what I have so far for my method (I think it's logically ok, but it's incorrect): </p>
  
  <pre><code> public static void runningSum(IntList l)
  { 
       l.setValue(l.sumList());
  
      while(l.getNext() != null) 
       {
          l.setNext(l.getNext()); //Set Next to be the next reference
          l.getValue();  //Get the Next's value
          l.setValue(l.sumList()); //Add the rest of the numbers together
       }
  
       if(l.getNext() == null)
       {
           l.setValue(l.getValue());
       }
  
       System.out.println(l.toString());
  }
  </code></pre>