Add Two Polynomials Represented as Linked Lists Problem
Description
LeetCode Problem 1634.
A polynomial linked list is a special type of linked list where every node represents a term in a polynomial expression.
Each node has three attributes:
coefficient: an integer representing the number multiplier of the term. The coefficient of the term**9**x^4is9.power: an integer representing the exponent. The power of the term9x**^4**is4.next: a pointer to the next node in the list, ornullif it is the last node of the list.
For example, the polynomial 5x^3 + 4x - 7 is represented by the polynomial linked list illustrated below:

The polynomial linked list must be in its standard form: the polynomial must be in strictly descending order by its power value. Also, terms with a coefficient of 0 are omitted.
Given two polynomial linked list heads, poly1 and poly2, add the polynomials together and return the head of the sum of the polynomials.
PolyNode format:
The input/output format is as a list of n nodes, where each node is represented as its [coefficient, power]. For example, the polynomial 5x^3 + 4x - 7 would be represented as: [[5,3],[4,1],[-7,0]].
Example 1:

Input: poly1 = [[1,1]], poly2 = [[1,0]]
Output: [[1,1],[1,0]]
Explanation: poly1 = x. poly2 = 1. The sum is x + 1.
Example 2:
Input: poly1 = [[2,2],[4,1],[3,0]], poly2 = [[3,2],[-4,1],[-1,0]]
Output: [[5,2],[2,0]]
Explanation: poly1 = 2x^2 + 4x + 3. poly2 = 3x^2 - 4x - 1. The sum is 5x^2 + 2. Notice that we omit the "0x" term.
Example 3:
Input: poly1 = [[1,2]], poly2 = [[-1,2]]
Output: []
Explanation: The sum is 0. We return an empty list.
Constraints:
0 <= n <= 10^4-10^9 <= PolyNode.coefficient <= 10^9PolyNode.coefficient != 00 <= PolyNode.power <= 10^9PolyNode.power > PolyNode.next.power
Solution
Approach: Merge the Two Lists
Both lists are sorted by power in strictly decreasing order, so adding them works like merging two sorted lists. Keep a pointer into each list and a tail for the result (a dummy node in front saves a special case for the head):
- If one current term has the larger power, no term in the other list has that power, so it goes to the result unchanged. Move that pointer on.
- If the powers are equal, add the coefficients. Add a node only when the sum is not
0, since a zero term must be left out, and move both pointers on.
When one list runs out, the rest of the other one is already in standard form, so link it to the end as it is.
The case to watch is cancellation: x^2 plus -x^2 leaves no node at all, and if every term cancels the answer is an empty list (null).
Complexity: O(m + n) time for lists of lengths m and n; O(1) extra space apart from the new nodes for added terms.
# Definition for polynomial singly-linked list.
# class PolyNode:
# def __init__(self, x=0, y=0, next=None):
# self.coefficient = x
# self.power = y
# self.next = next
class Solution:
def addPoly(self, poly1: 'PolyNode', poly2: 'PolyNode') -> 'PolyNode':
dummy = tail = PolyNode()
while poly1 and poly2:
if poly1.power > poly2.power:
tail.next = poly1
tail = tail.next
poly1 = poly1.next
elif poly1.power < poly2.power:
tail.next = poly2
tail = tail.next
poly2 = poly2.next
else:
c = poly1.coefficient + poly2.coefficient
if c != 0: # a term that cancels out is left out
tail.next = PolyNode(c, poly1.power)
tail = tail.next
poly1 = poly1.next
poly2 = poly2.next
tail.next = poly1 or poly2 # the rest of the longer list is already in order
return dummy.next