-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
39 lines (34 loc) · 1.1 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* 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;
auto p = &dummy;
auto p1 = poly1;
auto p2 = poly2;
while (p1 || p2) {
int power = 0;
if (p1) power = max(power, p1->power);
if (p2) power = max(power, p2->power);
int coefficient = 0;
if (p1 && power == p1->power)
coefficient += p1->coefficient, p1 = p1->next;
if (p2 && power == p2->power)
coefficient += p2->coefficient, p2 = p2->next;
if (coefficient == 0) continue;
p->next = new PolyNode(coefficient, power);
p = p->next;
}
return dummy.next;
}
};