Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to add two polynomials given as linked lists using Python
Suppose we are given two polynomials represented as linked lists and we need to find their addition. Each polynomial is stored as a linked list where each node contains a coefficient, power, and pointer to the next node. We need to return a new linked list representing the sum of the two polynomials.
For example, if we have polynomials 1x^1 + 1x^2 and 2x^1 + 3x^0, then the output will be 3x^1 + 1x^2 + 3x^0.
Algorithm
To solve this, we will follow these steps ?
Create dummy and node pointers for the result
-
While both polynomials have terms:
If power of poly1 > power of poly2, add poly1's term to result
If power of poly1 < power of poly2, add poly2's term to result
If powers are equal, add coefficients and create new term if non-zero
Add remaining terms from either polynomial
Return the result linked list
Implementation
class Polynomial:
def __init__(self, coeff=0, pow=0, nxt=None):
self.coefficient = coeff
self.power = pow
self.next = nxt
def create_poly(expression):
head = None
for element in expression:
if head == None:
head = Polynomial(element[0], element[1])
else:
temp = head
while temp.next != None:
temp = temp.next
temp.next = Polynomial(element[0], element[1])
return head
def show_poly(head):
temp = head
terms = []
while temp != None:
terms.append(str(temp.coefficient) + 'x^' + str(temp.power))
temp = temp.next
print(' + '.join(terms) + ' = 0')
def solve(poly1, poly2):
dummy = node = Polynomial()
while poly1 and poly2:
if poly1.power > poly2.power:
node.next = Polynomial(poly1.coefficient, poly1.power)
node = node.next
poly1 = poly1.next
elif poly1.power < poly2.power:
node.next = Polynomial(poly2.coefficient, poly2.power)
node = node.next
poly2 = poly2.next
else:
coef = poly1.coefficient + poly2.coefficient
if coef:
node.next = Polynomial(coef, poly1.power)
node = node.next
poly1 = poly1.next
poly2 = poly2.next
# Add remaining terms
while poly1:
node.next = Polynomial(poly1.coefficient, poly1.power)
node = node.next
poly1 = poly1.next
while poly2:
node.next = Polynomial(poly2.coefficient, poly2.power)
node = node.next
poly2 = poly2.next
return dummy.next
# Create polynomials
poly1 = create_poly([[1,1], [1,2]])
poly2 = create_poly([[2,1], [3,0]])
# Add polynomials
result = solve(poly1, poly2)
# Display result
print("First polynomial:")
show_poly(poly1)
print("Second polynomial:")
show_poly(poly2)
print("Sum:")
show_poly(result)
First polynomial: 1x^1 + 1x^2 = 0 Second polynomial: 2x^1 + 3x^0 = 0 Sum: 3x^1 + 1x^2 + 3x^0 = 0
How It Works
The algorithm compares the powers of terms from both polynomials:
Higher power: Copy the term with higher power directly to result
Lower power: Copy the term with lower power directly to result
Equal powers: Add coefficients and create new term if sum is non-zero
After processing both polynomials, any remaining terms are added to the result.
Conclusion
This solution efficiently adds two polynomials represented as linked lists by comparing powers and combining like terms. The time complexity is O(m+n) where m and n are the number of terms in each polynomial.
