-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.cpp
44 lines (43 loc) · 1.34 KB
/
Solution.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
40
41
42
43
44
/**
* 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 = new PolyNode();
PolyNode* curr = dummy;
while (poly1 && poly2) {
if (poly1->power > poly2->power) {
curr->next = poly1;
poly1 = poly1->next;
curr = curr->next;
} else if (poly1->power < poly2->power) {
curr->next = poly2;
poly2 = poly2->next;
curr = curr->next;
} else {
int c = poly1->coefficient + poly2->coefficient;
if (c != 0) {
curr->next = new PolyNode(c, poly1->power);
curr = curr->next;
}
poly1 = poly1->next;
poly2 = poly2->next;
}
}
if (!poly1) {
curr->next = poly2;
}
if (!poly2) {
curr->next = poly1;
}
return dummy->next;
}
};