72
72
73
73
<!-- 这里可写通用的实现逻辑 -->
74
74
75
- 遍历两多项式链表,比较节点间的 power 值,进行节点串联。若两节点 coefficient 值相加和为 0,不串联此合并的节点。
75
+ ** 方法一:遍历链表**
76
+
77
+ 我们可以同时遍历两个链表,根据指数大小关系,将节点添加到结果链表中。
78
+
79
+ 最后,如果链表 $1$ 或链表 $2$ 还有剩余节点,将其添加到结果链表中。
80
+
81
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为两个链表中节点数的较大值。
76
82
77
83
<!-- tabs:start -->
78
84
90
96
91
97
92
98
class Solution :
93
- def addPoly (self , poly1 : ' PolyNode' , poly2 : ' PolyNode' ) -> ' PolyNode' :
94
- dummy = PolyNode()
95
- cur = dummy
96
- while poly1 or poly2:
97
- if poly1 is None or (poly2 and poly2.power > poly1.power):
98
- cur.next = poly2
99
- cur = cur.next
100
- poly2 = poly2.next
101
- elif poly2 is None or (poly1 and poly1.power > poly2.power):
102
- cur.next = poly1
103
- cur = cur.next
99
+ def addPoly (self , poly1 : " PolyNode" , poly2 : " PolyNode" ) -> " PolyNode" :
100
+ dummy = curr = PolyNode()
101
+ while poly1 and poly2:
102
+ if poly1.power > poly2.power:
103
+ curr.next = poly1
104
104
poly1 = poly1.next
105
+ curr = curr.next
106
+ elif poly1.power < poly2.power:
107
+ curr.next = poly2
108
+ poly2 = poly2.next
109
+ curr = curr.next
105
110
else :
106
- val = poly1.coefficient + poly2.coefficient
107
- if val != 0 :
108
- cur.next = PolyNode(x = val, y = poly1.power)
109
- cur = cur.next
111
+ if c := poly1.coefficient + poly2.coefficient:
112
+ curr.next = PolyNode(c, poly1.power)
113
+ curr = curr.next
110
114
poly1 = poly1.next
111
115
poly2 = poly2.next
112
- cur .next = None
116
+ curr .next = poly1 or poly2
113
117
return dummy.next
114
118
```
115
119
@@ -126,40 +130,188 @@ class Solution:
126
130
127
131
* PolyNode() {}
128
132
* PolyNode(int x, int y) { this.coefficient = x; this.power = y; }
129
- * PolyNode(int x, int y, PolyNode next) { this.coefficient = x; this.power = y; this.next =
130
- next; }
133
+ * PolyNode(int x, int y, PolyNode next) { this.coefficient = x; this.power = y; this.next = next; }
131
134
* }
132
135
*/
133
136
134
- class Solution {
137
+ class Solution {
135
138
public PolyNode addPoly (PolyNode poly1 , PolyNode poly2 ) {
136
139
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;
140
+ PolyNode curr = dummy;
141
+ while (poly1 != null && poly2 != null ) {
142
+ if (poly1. power > poly2. power) {
143
+ curr. next = poly1;
144
+ poly1 = poly1. next;
145
+ curr = curr. next;
146
+ } else if (poly1. power < poly2. power) {
147
+ curr. next = poly2;
142
148
poly2 = poly2. next;
143
- } else if (poly2 == null || (poly1 != null && poly1. power > poly2. power)) {
144
- cur. next = poly1;
145
- cur = cur. next;
149
+ curr = curr. next;
150
+ } else {
151
+ int c = poly1. coefficient + poly2. coefficient;
152
+ if (c != 0 ) {
153
+ curr. next = new PolyNode (c, poly1. power);
154
+ curr = curr. next;
155
+ }
156
+ poly1 = poly1. next;
157
+ poly2 = poly2. next;
158
+ }
159
+ }
160
+ if (poly1 == null ) {
161
+ curr. next = poly2;
162
+ }
163
+ if (poly2 == null ) {
164
+ curr. next = poly1;
165
+ }
166
+ return dummy. next;
167
+ }
168
+ }
169
+ ```
170
+
171
+ ### ** C++**
172
+
173
+ ``` cpp
174
+ /* *
175
+ * Definition for polynomial singly-linked list->
176
+ * struct PolyNode {
177
+ * int coefficient, power;
178
+ * PolyNode *next;
179
+ * PolyNode(): coefficient(0), power(0), next(nullptr) {};
180
+ * PolyNode(int x, int y): coefficient(x), power(y), next(nullptr) {};
181
+ * PolyNode(int x, int y, PolyNode* next): coefficient(x), power(y), next(next) {};
182
+ * };
183
+ */
184
+
185
+ class Solution {
186
+ public:
187
+ PolyNode* addPoly(PolyNode* poly1, PolyNode* poly2) {
188
+ PolyNode* dummy = new PolyNode();
189
+ PolyNode* curr = dummy;
190
+ while (poly1 && poly2) {
191
+ if (poly1->power > poly2->power) {
192
+ curr->next = poly1;
193
+ poly1 = poly1->next;
194
+ curr = curr->next;
195
+ } else if (poly1->power < poly2->power) {
196
+ curr->next = poly2;
197
+ poly2 = poly2->next;
198
+ curr = curr->next;
199
+ } else {
200
+ int c = poly1->coefficient + poly2->coefficient;
201
+ if (c != 0) {
202
+ curr->next = new PolyNode(c, poly1->power);
203
+ curr = curr->next;
204
+ }
205
+ poly1 = poly1->next;
206
+ poly2 = poly2->next;
207
+ }
208
+ }
209
+ if (!poly1) {
210
+ curr->next = poly2;
211
+ }
212
+ if (!poly2) {
213
+ curr->next = poly1;
214
+ }
215
+ return dummy->next;
216
+ }
217
+ };
218
+ ```
219
+
220
+ ### **C#**
221
+
222
+ ```cs
223
+ /**
224
+ * Definition for polynomial singly-linked list.
225
+ * public class PolyNode {
226
+ * public int coefficient, power;
227
+ * public PolyNode next;
228
+ *
229
+ * public PolyNode(int x=0, int y=0, PolyNode next=null) {
230
+ * this.coefficient = x;
231
+ * this.power = y;
232
+ * this.next = next;
233
+ * }
234
+ * }
235
+ */
236
+
237
+ public class Solution {
238
+ public PolyNode AddPoly(PolyNode poly1, PolyNode poly2) {
239
+ PolyNode dummy = new PolyNode();
240
+ PolyNode curr = dummy;
241
+ while (poly1 != null && poly2 != null) {
242
+ if (poly1.power > poly2.power) {
243
+ curr.next = poly1;
146
244
poly1 = poly1.next;
245
+ curr = curr.next;
246
+ } else if (poly1.power < poly2.power) {
247
+ curr.next = poly2;
248
+ poly2 = poly2.next;
249
+ curr = curr.next;
147
250
} else {
148
- int val = poly1. coefficient + poly2. coefficient;
149
- if (val != 0 ) {
150
- cur . next = new PolyNode (val , poly1. power);
151
- cur = cur . next;
251
+ int c = poly1.coefficient + poly2.coefficient;
252
+ if (c != 0) {
253
+ curr .next = new PolyNode(c , poly1.power);
254
+ curr = curr .next;
152
255
}
153
256
poly1 = poly1.next;
154
257
poly2 = poly2.next;
155
258
}
156
259
}
157
- cur. next = null ;
260
+ if (poly1 == null) {
261
+ curr.next = poly2;
262
+ }
263
+ if (poly2 == null) {
264
+ curr.next = poly1;
265
+ }
158
266
return dummy.next;
159
267
}
160
268
}
161
269
```
162
270
271
+ ### ** JavaScript**
272
+
273
+ ``` js
274
+ /**
275
+ * Definition for polynomial singly-linked list.
276
+ * function PolyNode(x=0, y=0, next=null) {
277
+ * this.coefficient = x;
278
+ * this.power = y;
279
+ * this.next = next;
280
+ * }
281
+ */
282
+
283
+ /**
284
+ * @param {PolyNode} poly1
285
+ * @param {PolyNode} poly2
286
+ * @return {PolyNode}
287
+ */
288
+ var addPoly = function (poly1 , poly2 ) {
289
+ const dummy = new PolyNode ();
290
+ let curr = dummy;
291
+ while (poly1 && poly2) {
292
+ if (poly1 .power > poly2 .power ) {
293
+ curr .next = poly1;
294
+ poly1 = poly1 .next ;
295
+ curr = curr .next ;
296
+ } else if (poly1 .power < poly2 .power ) {
297
+ curr .next = poly2;
298
+ poly2 = poly2 .next ;
299
+ curr = curr .next ;
300
+ } else {
301
+ const c = poly1 .coefficient + poly2 .coefficient ;
302
+ if (c != 0 ) {
303
+ curr .next = new PolyNode (c, poly1 .power );
304
+ curr = curr .next ;
305
+ }
306
+ poly1 = poly1 .next ;
307
+ poly2 = poly2 .next ;
308
+ }
309
+ }
310
+ curr .next = poly1 || poly2;
311
+ return dummy .next ;
312
+ };
313
+ ```
314
+
163
315
### ** ...**
164
316
165
317
```
0 commit comments