Algorithm


Addition of Two Polynomials Using Linked List:

  1. Define a Node structure with fields for coefficient, exponent, and a pointer to the next node.
  2. Create two linked lists to represent the input polynomials.
  3. Initialize a new linked list to store the result polynomial.
  4. Set pointers to the head of both input linked lists.
  5. While neither linked list is fully traversed:
    1. Compare the exponents of the current nodes from both linked lists.
    2. If the exponents are equal:
      1. Add the coefficients of the nodes.
      2. Create a new node with the sum of coefficients and the common exponent.
      3. Append the new node to the result linked list.
      4. Move both pointers to the next node.
    3. If the exponent of the first polynomial is greater:
      1. Create a new node with the coefficient and exponent of the current node from the first polynomial.
      2. Append the new node to the result linked list.
      3. Move the pointer of the first polynomial to the next node.
    4. If the exponent of the second polynomial is greater:
      1. Create a new node with the coefficient and exponent of the current node from the second polynomial.
      2. Append the new node to the result linked list.
      3. Move the pointer of the second polynomial to the next node.
  6. Copy any remaining nodes from either linked list into the result linked list.
  7. Return the head of the result linked list.

Example:

Polynomial Addition Example

Fig. 1: Example of Adding Two Polynomials Using Linked List

Note: Ensure that the polynomials are sorted by descending exponents before performing the addition.