-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.java
45 lines (43 loc) · 1.32 KB
/
Solution.java
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
45
/**
* Definition for polynomial singly-linked list.
* class PolyNode {
* int coefficient, power;
* PolyNode next = null;
* 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) {
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 == null) {
curr.next = poly2;
}
if (poly2 == null) {
curr.next = poly1;
}
return dummy.next;
}
}