-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.cs
46 lines (45 loc) · 1.31 KB
/
Solution.cs
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
46
/**
* Definition for polynomial singly-linked list.
* public class PolyNode {
* public int coefficient, power;
* public PolyNode next;
*
* public PolyNode(int x=0, int y=0, PolyNode next=null) {
* this.coefficient = x;
* this.power = y;
* this.next = next;
* }
* }
*/
public 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;
}
}