-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.js
39 lines (38 loc) · 1016 Bytes
/
Solution.js
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.
* function PolyNode(x=0, y=0, next=null) {
* this.coefficient = x;
* this.power = y;
* this.next = next;
* }
*/
/**
* @param {PolyNode} poly1
* @param {PolyNode} poly2
* @return {PolyNode}
*/
var addPoly = function (poly1, poly2) {
const dummy = new PolyNode();
let 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 {
const c = poly1.coefficient + poly2.coefficient;
if (c != 0) {
curr.next = new PolyNode(c, poly1.power);
curr = curr.next;
}
poly1 = poly1.next;
poly2 = poly2.next;
}
}
curr.next = poly1 || poly2;
return dummy.next;
};