Skip to content

Commit f346eed

Browse files
committed
feat: add solutions to lc problem: No.1634. Add Two Polynomials Represented as Linked Lists
1 parent d4ea666 commit f346eed

File tree

4 files changed

+206
-39
lines changed

4 files changed

+206
-39
lines changed

solution/1600-1699/1634.Add Two Polynomials Represented as Linked Lists/README.md

+70-3
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,94 @@
6868
<li><code>PolyNode.power > PolyNode.next.power</code></li>
6969
</ul>
7070

71-
7271
## 解法
7372

7473
<!-- 这里可写通用的实现逻辑 -->
7574

75+
遍历两多项式链表,比较节点间的 power 值,进行节点串联。若两节点 coefficient 值相加和为 0,不串联此合并的节点。
76+
7677
<!-- tabs:start -->
7778

7879
### **Python3**
7980

8081
<!-- 这里可写当前语言的特殊实现逻辑 -->
8182

8283
```python
83-
84+
# Definition for polynomial singly-linked list.
85+
# class PolyNode:
86+
# def __init__(self, x=0, y=0, next=None):
87+
# self.coefficient = x
88+
# self.power = y
89+
# self.next = next
90+
91+
class Solution:
92+
def addPoly(self, poly1: 'PolyNode', poly2: 'PolyNode') -> 'PolyNode':
93+
dummy = PolyNode()
94+
cur = dummy
95+
while poly1 or poly2:
96+
if poly1 is None or (poly2 and poly2.power > poly1.power):
97+
cur.next = poly2
98+
cur = cur.next
99+
poly2 = poly2.next
100+
elif poly2 is None or (poly1 and poly1.power > poly2.power):
101+
cur.next = poly1
102+
cur = cur.next
103+
poly1 = poly1.next
104+
else:
105+
val = poly1.coefficient + poly2.coefficient
106+
if val != 0:
107+
cur.next = PolyNode(x=val, y=poly1.power)
108+
cur = cur.next
109+
poly1 = poly1.next
110+
poly2 = poly2.next
111+
cur.next = None
112+
return dummy.next
84113
```
85114

86115
### **Java**
87116

88117
<!-- 这里可写当前语言的特殊实现逻辑 -->
89118

90119
```java
91-
120+
/**
121+
* Definition for polynomial singly-linked list.
122+
* class PolyNode {
123+
* int coefficient, power;
124+
* PolyNode next = null;
125+
126+
* PolyNode() {}
127+
* PolyNode(int x, int y) { this.coefficient = x; this.power = y; }
128+
* PolyNode(int x, int y, PolyNode next) { this.coefficient = x; this.power = y; this.next = next; }
129+
* }
130+
*/
131+
132+
class Solution {
133+
public PolyNode addPoly(PolyNode poly1, PolyNode poly2) {
134+
PolyNode dummy = new PolyNode();
135+
PolyNode cur = dummy;
136+
while (poly1 != null || poly2 != null) {
137+
if (poly1 == null || (poly2 != null && poly2.power > poly1.power)) {
138+
cur.next = poly2;
139+
cur = cur.next;
140+
poly2 = poly2.next;
141+
} else if (poly2 == null || (poly1 != null && poly1.power > poly2.power)) {
142+
cur.next = poly1;
143+
cur = cur.next;
144+
poly1 = poly1.next;
145+
} else {
146+
int val = poly1.coefficient + poly2.coefficient;
147+
if (val != 0) {
148+
cur.next = new PolyNode(val, poly1.power);
149+
cur = cur.next;
150+
}
151+
poly1 = poly1.next;
152+
poly2 = poly2.next;
153+
}
154+
}
155+
cur.next = null;
156+
return dummy.next;
157+
}
158+
}
92159
```
93160

94161
### **...**

solution/1600-1699/1634.Add Two Polynomials Represented as Linked Lists/README_EN.md

+68-36
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,32 @@
66

77
<p>A polynomial linked list is a special type of linked list where every node represents a term in a polynomial expression.</p>
88

9-
10-
119
<p>Each node has three attributes:</p>
1210

13-
14-
1511
<ul>
1612
<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>
1713
<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>
1814
<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>
1915
</ul>
2016

21-
22-
2317
<p>For example, the polynomial <code>5x<sup>3</sup> + 4x - 7</code> is represented by the polynomial linked list illustrated below:</p>
2418

25-
26-
2719
<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>
2820

29-
30-
3121
<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>
3222

33-
34-
3523
<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>
3624

37-
38-
3925
<p><strong><code>PolyNode</code> format:</strong></p>
4026

41-
42-
4327
<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>
4428

45-
46-
4729
<p>&nbsp;</p>
4830

4931
<p><strong>Example 1:</strong></p>
5032

51-
52-
5333
<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>
5434

55-
56-
5735
<pre>
5836

5937
<strong>Input:</strong> poly1 = [[1,1]], poly2 = [[1,0]]
@@ -64,12 +42,8 @@
6442

6543
</pre>
6644

67-
68-
6945
<p><strong>Example 2:</strong></p>
7046

71-
72-
7347
<pre>
7448

7549
<strong>Input:</strong> poly1 = [[2,2],[4,1],[3,0]], poly2 = [[3,2],[-4,1],[-1,0]]
@@ -80,12 +54,8 @@
8054

8155
</pre>
8256

83-
84-
8557
<p><strong>Example 3:</strong></p>
8658

87-
88-
8959
<pre>
9060

9161
<strong>Input:</strong> poly1 = [[1,2]], poly2 = [[-1,2]]
@@ -96,14 +66,10 @@
9666

9767
</pre>
9868

99-
100-
10169
<p>&nbsp;</p>
10270

10371
<p><strong>Constraints:</strong></p>
10472

105-
106-
10773
<ul>
10874
<li><code>0 &lt;= n &lt;= 10<sup>4</sup></code></li>
10975
<li><code>-10<sup>9</sup>&nbsp;&lt;= PolyNode.coefficient &lt;= 10<sup>9</sup></code></li>
@@ -119,13 +85,79 @@
11985
### **Python3**
12086

12187
```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
123117
```
124118

125119
### **Java**
126120

127121
```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+
}
129161
```
130162

131163
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Definition for polynomial singly-linked list.
3+
* class PolyNode {
4+
* int coefficient, power;
5+
* PolyNode next = null;
6+
7+
* PolyNode() {}
8+
* PolyNode(int x, int y) { this.coefficient = x; this.power = y; }
9+
* PolyNode(int x, int y, PolyNode next) { this.coefficient = x; this.power = y; this.next = next; }
10+
* }
11+
*/
12+
13+
class Solution {
14+
public PolyNode addPoly(PolyNode poly1, PolyNode poly2) {
15+
PolyNode dummy = new PolyNode();
16+
PolyNode cur = dummy;
17+
while (poly1 != null || poly2 != null) {
18+
if (poly1 == null || (poly2 != null && poly2.power > poly1.power)) {
19+
cur.next = poly2;
20+
cur = cur.next;
21+
poly2 = poly2.next;
22+
} else if (poly2 == null || (poly1 != null && poly1.power > poly2.power)) {
23+
cur.next = poly1;
24+
cur = cur.next;
25+
poly1 = poly1.next;
26+
} else {
27+
int val = poly1.coefficient + poly2.coefficient;
28+
if (val != 0) {
29+
cur.next = new PolyNode(val, poly1.power);
30+
cur = cur.next;
31+
}
32+
poly1 = poly1.next;
33+
poly2 = poly2.next;
34+
}
35+
}
36+
cur.next = null;
37+
return dummy.next;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Definition for polynomial singly-linked list.
2+
# class PolyNode:
3+
# def __init__(self, x=0, y=0, next=None):
4+
# self.coefficient = x
5+
# self.power = y
6+
# self.next = next
7+
8+
class Solution:
9+
def addPoly(self, poly1: 'PolyNode', poly2: 'PolyNode') -> 'PolyNode':
10+
dummy = PolyNode()
11+
cur = dummy
12+
while poly1 or poly2:
13+
if poly1 is None or (poly2 and poly2.power > poly1.power):
14+
cur.next = poly2
15+
cur = cur.next
16+
poly2 = poly2.next
17+
elif poly2 is None or (poly1 and poly1.power > poly2.power):
18+
cur.next = poly1
19+
cur = cur.next
20+
poly1 = poly1.next
21+
else:
22+
val = poly1.coefficient + poly2.coefficient
23+
if val != 0:
24+
cur.next = PolyNode(x=val, y=poly1.power)
25+
cur = cur.next
26+
poly1 = poly1.next
27+
poly2 = poly2.next
28+
cur.next = None
29+
return dummy.next

0 commit comments

Comments
 (0)