title: Combining Like Terms/ LinkedList Polynomial [Java]
body: <p>So my issue here is getting this program to combine like terms, I've tried about 3 different ways of going about this and cannot figure it out. I can sort the array in somewhat the correct order but not get it to combine. </p>
  
  <pre><code>    public static Node multiply(Node poly1, Node poly2) {
      Node polynomial1 = poly1;
      Node polynomial2 = poly2;
      Node mult = poly2;
      Node temp3 = null;
      float cmb = 0;
      int cmbExp = 0; 
  
      // the sum of the polynomials
      Node temp = null;
      float coeffProd;
      int expSum;
  
      if(poly1 == null)
          return "Enter a Different Polynomial";
  
      if(poly2 == null)
          return "Enter a Different Polynomial";
  
  
      // while loop if either polynomial runs out of terms. This avoids nullpointerexception
      while (polynomial1 != null) {
          while(polynomial2 != null){
  
              coeffProd = polynomial1.term.coeff * polynomial2.term.coeff;
              expSum = polynomial1.term.degree + polynomial2.term.degree;
              temp = new Node(coeffProd, expSum, temp);
  
              polynomial2 = polynomial2.next;
          }
          polynomial1 = polynomial1.next;
          polynomial2 = mult; 
  
       }
      // my attempt at combining
      Node temp2 = temp.next;
      while(temp != null){
          while(temp2 != null){
              if(temp.term.degree == temp2.term.degree){
                  cmb = temp.term.coeff + temp2.term.coeff;
                  cmbExp = temp.term.degree;
             }
              temp2 = temp2.next;
              temp3 = new Node(cmb,cmbExp,temp3);
          }
          temp = temp.next;
      }
  // Reverses the Polynomial into the correct sequence
      Node flip = temp;
      Node ans = null;
      while (flip != null)
      {
          if (flip.term.coeff == 0)
          {
              flip = flip.next;
              continue;
          }
          else
          {
              ans = new Node(flip.term.coeff, flip.term.degree, ans);
              flip = flip.next;
          }
      }
  
      if (ans == null)
          return null;
      else
      {
     return ans;
      }  
  }
  </code></pre>
  
  <p>Result:
  32.0x^9 + 16.0x^8 + -12.0x^6 + 36.0x^5 + -16.0x^7 + -8.0x^6 + 6.0x^4 + -18.0x^3 + 16.0x^5 + 8.0x^4 + -6.0x^2 + 18.0x + 24.0x^4 + 12.0x^3 + -9.0x + 27.0</p>
  
  <p>I need to combine these like terms. </p>