# Singly Linked List Java implementation

• 08-31-2011, 07:59 AM
xayto
Ok for a Uni assignment I am being requested to construct a Polynomial Class with various methods. I get the general jist of what I need to accomplish but I am having trouble with one method.

What I am having trouble with is where I need to add a new node to the Start of a linked list, I am not able to using the code below. I only get the original value of the polynomial Returned.

Any prods in the right direction would be great!

Code:

```        public static Polynomial add(Polynomial polynomial, double coefficient, int power)         {                 //Check validity of Polynomial                 if (polynomial == null) return null;                 if (Math.abs(coefficient) < epsilon) coefficient = 0.0;                 if (coefficient == 0.0) return null;                 if (power < 0) return null;                                 //If power is less than smallest node add as last node (works)                 if (power < polynomial.powerMin())                   {                         Polynomial newNode = new Polynomial(coefficient,power);                         if (polynomial.successor != null)                         {                                 while (polynomial.successor != null)                                 {                                         polynomial.successor = polynomial.successor.successor;                                 }                                 polynomial.successor = newNode;                         }                                                 else if (polynomial.successor == null )                                 polynomial.successor = newNode;                                           }                                 //if power is greater than the first node add to the front (Doesn't work)                 else if (power > polynomial.powerMax())                         if (polynomial == null)                         {                                 Polynomial newNode = new Polynomial(coefficient,power);                                 polynomial = newNode;                         }                         else{                 {                         Polynomial newNode = new Polynomial(coefficient,power);                         newNode.successor = polynomial;                         polynomial = newNode;                         return polynomial;                                         }                         }                 //where power is the same in both nodes, add coefficients and leave power alone (works)                 else                 {                 if (power == polynomial.power)                         polynomial.coefficient = polynomial.coefficient + coefficient;                                                           }                 return polynomial;                         }```
• 08-31-2011, 01:36 PM
Norm
Quote:

I need to add a new node to the Start of a linked list,
When adding to the beginning of a linked list, how do you set the list's pointer to include the new node?

Quote:

I only get the original value of the polynomial Returned.
What is your method supposed to return?