|
6 | 6 |
|
7 | 7 | <p>A polynomial linked list is a special type of linked list where every node represents a term in a polynomial expression.</p>
|
8 | 8 |
|
9 |
| - |
10 |
| - |
11 | 9 | <p>Each node has three attributes:</p>
|
12 | 10 |
|
13 |
| - |
14 |
| - |
15 | 11 | <ul>
|
16 | 12 | <li><code>coefficient</code>: an integer representing the number multiplier of the term. The coefficient of the term <code><strong>9</strong>x<sup>4</sup></code> is <code>9</code>.</li>
|
17 | 13 | <li><code>power</code>: an integer representing the exponent. The power of the term <code>9x<strong><sup>4</sup></strong></code> is <code>4</code>.</li>
|
18 | 14 | <li><code>next</code>: a pointer to the next node in the list, or <code>null</code> if it is the last node of the list.</li>
|
19 | 15 | </ul>
|
20 | 16 |
|
21 |
| - |
22 |
| - |
23 | 17 | <p>For example, the polynomial <code>5x<sup>3</sup> + 4x - 7</code> is represented by the polynomial linked list illustrated below:</p>
|
24 | 18 |
|
25 |
| - |
26 |
| - |
27 | 19 | <p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1600-1699/1634.Add%20Two%20Polynomials%20Represented%20as%20Linked%20Lists/images/polynomial2.png" style="width: 500px; height: 91px;" /></p>
|
28 | 20 |
|
29 |
| - |
30 |
| - |
31 | 21 | <p>The polynomial linked list must be in its standard form: the polynomial must be in <strong>strictly</strong> descending order by its <code>power</code> value. Also, terms with a <code>coefficient</code> of <code>0</code> are omitted.</p>
|
32 | 22 |
|
33 |
| - |
34 |
| - |
35 | 23 | <p>Given two polynomial linked list heads, <code>poly1</code> and <code>poly2</code>, add the polynomials together and return <em>the head of the sum of the polynomials</em>.</p>
|
36 | 24 |
|
37 |
| - |
38 |
| - |
39 | 25 | <p><strong><code>PolyNode</code> format:</strong></p>
|
40 | 26 |
|
41 |
| - |
42 |
| - |
43 | 27 | <p>The input/output format is as a list of <code>n</code> nodes, where each node is represented as its <code>[coefficient, power]</code>. For example, the polynomial <code>5x<sup>3</sup> + 4x - 7</code> would be represented as: <code>[[5,3],[4,1],[-7,0]]</code>.</p>
|
44 | 28 |
|
45 |
| - |
46 |
| - |
47 | 29 | <p> </p>
|
48 | 30 |
|
49 | 31 | <p><strong>Example 1:</strong></p>
|
50 | 32 |
|
51 |
| - |
52 |
| - |
53 | 33 | <p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1600-1699/1634.Add%20Two%20Polynomials%20Represented%20as%20Linked%20Lists/images/ex1.png" style="width: 600px; height: 322px;" /></p>
|
54 | 34 |
|
55 |
| - |
56 |
| - |
57 | 35 | <pre>
|
58 | 36 |
|
59 | 37 | <strong>Input:</strong> poly1 = [[1,1]], poly2 = [[1,0]]
|
|
64 | 42 |
|
65 | 43 | </pre>
|
66 | 44 |
|
67 |
| - |
68 |
| - |
69 | 45 | <p><strong>Example 2:</strong></p>
|
70 | 46 |
|
71 |
| - |
72 |
| - |
73 | 47 | <pre>
|
74 | 48 |
|
75 | 49 | <strong>Input:</strong> poly1 = [[2,2],[4,1],[3,0]], poly2 = [[3,2],[-4,1],[-1,0]]
|
|
80 | 54 |
|
81 | 55 | </pre>
|
82 | 56 |
|
83 |
| - |
84 |
| - |
85 | 57 | <p><strong>Example 3:</strong></p>
|
86 | 58 |
|
87 |
| - |
88 |
| - |
89 | 59 | <pre>
|
90 | 60 |
|
91 | 61 | <strong>Input:</strong> poly1 = [[1,2]], poly2 = [[-1,2]]
|
|
96 | 66 |
|
97 | 67 | </pre>
|
98 | 68 |
|
99 |
| - |
100 |
| - |
101 | 69 | <p> </p>
|
102 | 70 |
|
103 | 71 | <p><strong>Constraints:</strong></p>
|
104 | 72 |
|
105 |
| - |
106 |
| - |
107 | 73 | <ul>
|
108 | 74 | <li><code>0 <= n <= 10<sup>4</sup></code></li>
|
109 | 75 | <li><code>-10<sup>9</sup> <= PolyNode.coefficient <= 10<sup>9</sup></code></li>
|
|
119 | 85 | ### **Python3**
|
120 | 86 |
|
121 | 87 | ```python
|
122 |
| - |
| 88 | +# Definition for polynomial singly-linked list. |
| 89 | +# class PolyNode: |
| 90 | +# def __init__(self, x=0, y=0, next=None): |
| 91 | +# self.coefficient = x |
| 92 | +# self.power = y |
| 93 | +# self.next = next |
| 94 | + |
| 95 | +class Solution: |
| 96 | + def addPoly(self, poly1: 'PolyNode', poly2: 'PolyNode') -> 'PolyNode': |
| 97 | + dummy = PolyNode() |
| 98 | + cur = dummy |
| 99 | + while poly1 or poly2: |
| 100 | + if poly1 is None or (poly2 and poly2.power > poly1.power): |
| 101 | + cur.next = poly2 |
| 102 | + cur = cur.next |
| 103 | + poly2 = poly2.next |
| 104 | + elif poly2 is None or (poly1 and poly1.power > poly2.power): |
| 105 | + cur.next = poly1 |
| 106 | + cur = cur.next |
| 107 | + poly1 = poly1.next |
| 108 | + else: |
| 109 | + val = poly1.coefficient + poly2.coefficient |
| 110 | + if val != 0: |
| 111 | + cur.next = PolyNode(x=val, y=poly1.power) |
| 112 | + cur = cur.next |
| 113 | + poly1 = poly1.next |
| 114 | + poly2 = poly2.next |
| 115 | + cur.next = None |
| 116 | + return dummy.next |
123 | 117 | ```
|
124 | 118 |
|
125 | 119 | ### **Java**
|
126 | 120 |
|
127 | 121 | ```java
|
128 |
| - |
| 122 | +/** |
| 123 | + * Definition for polynomial singly-linked list. |
| 124 | + * class PolyNode { |
| 125 | + * int coefficient, power; |
| 126 | + * PolyNode next = null; |
| 127 | + |
| 128 | + * PolyNode() {} |
| 129 | + * PolyNode(int x, int y) { this.coefficient = x; this.power = y; } |
| 130 | + * PolyNode(int x, int y, PolyNode next) { this.coefficient = x; this.power = y; this.next = next; } |
| 131 | + * } |
| 132 | + */ |
| 133 | + |
| 134 | +class Solution { |
| 135 | + public PolyNode addPoly(PolyNode poly1, PolyNode poly2) { |
| 136 | + PolyNode dummy = new PolyNode(); |
| 137 | + PolyNode cur = dummy; |
| 138 | + while (poly1 != null || poly2 != null) { |
| 139 | + if (poly1 == null || (poly2 != null && poly2.power > poly1.power)) { |
| 140 | + cur.next = poly2; |
| 141 | + cur = cur.next; |
| 142 | + poly2 = poly2.next; |
| 143 | + } else if (poly2 == null || (poly1 != null && poly1.power > poly2.power)) { |
| 144 | + cur.next = poly1; |
| 145 | + cur = cur.next; |
| 146 | + poly1 = poly1.next; |
| 147 | + } else { |
| 148 | + int val = poly1.coefficient + poly2.coefficient; |
| 149 | + if (val != 0) { |
| 150 | + cur.next = new PolyNode(val, poly1.power); |
| 151 | + cur = cur.next; |
| 152 | + } |
| 153 | + poly1 = poly1.next; |
| 154 | + poly2 = poly2.next; |
| 155 | + } |
| 156 | + } |
| 157 | + cur.next = null; |
| 158 | + return dummy.next; |
| 159 | + } |
| 160 | +} |
129 | 161 | ```
|
130 | 162 |
|
131 | 163 | ### **...**
|
|
0 commit comments