# Definition for singly-linked list node for polynomial term.
class PolyNode:
def __init__(self, coefficient=0, power=0, next=None):
self.coefficient = coefficient
self.power = power
self.next = next
class Solution:
def addPoly(self, poly1: 'PolyNode', poly2: 'PolyNode') -> 'PolyNode':
dummy = PolyNode()
curr = dummy
while poly1 and poly2:
if poly1.power == poly2.power:
coef_sum = poly1.coefficient + poly2.coefficient
if coef_sum != 0:
curr.next = PolyNode(coef_sum, poly1.power)
curr = curr.next
poly1 = poly1.next
poly2 = poly2.next
elif poly1.power > poly2.power:
curr.next = PolyNode(poly1.coefficient, poly1.power)
curr = curr.next
poly1 = poly1.next
else:
curr.next = PolyNode(poly2.coefficient, poly2.power)
curr = curr.next
poly2 = poly2.next
# Append remaining terms
while poly1:
curr.next = PolyNode(poly1.coefficient, poly1.power)
curr = curr.next
poly1 = poly1.next
while poly2:
curr.next = PolyNode(poly2.coefficient, poly2.power)
curr = curr.next
poly2 = poly2.next
return dummy.next
/**
* Definition for polynomial singly-linked list.
* struct PolyNode {
* int coefficient, power;
* PolyNode *next;
* PolyNode(): coefficient(0), power(0), next(nullptr) {}
* PolyNode(int x, int y): coefficient(x), power(y), next(nullptr) {}
* PolyNode(int x, int y, PolyNode *next): coefficient(x), power(y), next(next) {}
* };
*/
class Solution {
public:
PolyNode* addPoly(PolyNode* poly1, PolyNode* poly2) {
PolyNode dummy(0, 0);
PolyNode* curr = &dummy;
while (poly1 && poly2) {
if (poly1->power == poly2->power) {
int coef = poly1->coefficient + poly2->coefficient;
if (coef != 0) {
curr->next = new PolyNode(coef, poly1->power);
curr = curr->next;
}
poly1 = poly1->next;
poly2 = poly2->next;
} else if (poly1->power > poly2->power) {
curr->next = new PolyNode(poly1->coefficient, poly1->power);
curr = curr->next;
poly1 = poly1->next;
} else {
curr->next = new PolyNode(poly2->coefficient, poly2->power);
curr = curr->next;
poly2 = poly2->next;
}
}
while (poly1) {
curr->next = new PolyNode(poly1->coefficient, poly1->power);
curr = curr->next;
poly1 = poly1->next;
}
while (poly2) {
curr->next = new PolyNode(poly2->coefficient, poly2->power);
curr = curr->next;
poly2 = poly2->next;
}
return dummy.next;
}
};
/**
* Definition for polynomial singly-linked list.
* class PolyNode {
* int coefficient, power;
* PolyNode next;
* PolyNode() {}
* PolyNode(int x, int y) { this.coefficient = x; this.power = y; }
* PolyNode(int x, int y, PolyNode next) { this.coefficient = x; this.power = y; this.next = next; }
* }
*/
class Solution {
public PolyNode addPoly(PolyNode poly1, PolyNode poly2) {
PolyNode dummy = new PolyNode();
PolyNode curr = dummy;
while (poly1 != null && poly2 != null) {
if (poly1.power == poly2.power) {
int coef = poly1.coefficient + poly2.coefficient;
if (coef != 0) {
curr.next = new PolyNode(coef, poly1.power);
curr = curr.next;
}
poly1 = poly1.next;
poly2 = poly2.next;
} else if (poly1.power > poly2.power) {
curr.next = new PolyNode(poly1.coefficient, poly1.power);
curr = curr.next;
poly1 = poly1.next;
} else {
curr.next = new PolyNode(poly2.coefficient, poly2.power);
curr = curr.next;
poly2 = poly2.next;
}
}
while (poly1 != null) {
curr.next = new PolyNode(poly1.coefficient, poly1.power);
curr = curr.next;
poly1 = poly1.next;
}
while (poly2 != null) {
curr.next = new PolyNode(poly2.coefficient, poly2.power);
curr = curr.next;
poly2 = poly2.next;
}
return dummy.next;
}
}
/**
* // Definition for singly-linked list for polynomial term.
* function PolyNode(coefficient, power, next) {
* this.coefficient = (coefficient===undefined ? 0 : coefficient)
* this.power = (power===undefined ? 0 : power)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {PolyNode} poly1
* @param {PolyNode} poly2
* @return {PolyNode}
*/
var addPoly = function(poly1, poly2) {
let dummy = new PolyNode(0, 0);
let curr = dummy;
while (poly1 && poly2) {
if (poly1.power === poly2.power) {
let coef = poly1.coefficient + poly2.coefficient;
if (coef !== 0) {
curr.next = new PolyNode(coef, poly1.power);
curr = curr.next;
}
poly1 = poly1.next;
poly2 = poly2.next;
} else if (poly1.power > poly2.power) {
curr.next = new PolyNode(poly1.coefficient, poly1.power);
curr = curr.next;
poly1 = poly1.next;
} else {
curr.next = new PolyNode(poly2.coefficient, poly2.power);
curr = curr.next;
poly2 = poly2.next;
}
}
while (poly1) {
curr.next = new PolyNode(poly1.coefficient, poly1.power);
curr = curr.next;
poly1 = poly1.next;
}
while (poly2) {
curr.next = new PolyNode(poly2.coefficient, poly2.power);
curr = curr.next;
poly2 = poly2.next;
}
return dummy.next;
};
coefficient
and a power
(the exponent), add the two polynomials and return the sum as a new linked list in the same format. The input lists are sorted in descending order of power
. Each term with a zero coefficient should be omitted from the result, and you must not reuse or modify the input nodes. There is always one valid solution.
power
field is like a sorting key. If both lists have a term with the same power
, add their coefficients. If the powers are different, keep the term with the larger power. This is similar to merging two sorted arrays or linked lists, but with the additional logic of summing coefficients for matching exponents and skipping zero-coefficient terms.poly1
is 5x3 + 4x2 + 2 and poly2
is 5x1 + 5.poly1
: (5,3) → (4,2) → (2,0)poly2
: (5,1) → (5,0)