0% found this document useful (0 votes)
3 views

6-7-Adding two polynomials using Linked List

The document discusses how to add two polynomials represented by linked lists, detailing both recursive and iterative approaches. It provides examples of polynomial addition and outlines the time and space complexities for each method. The recursive method has a time complexity of O(m+n) and space complexity of O(max(m,n)), while the iterative method also has a time complexity of O(m+n) but uses O(1) space.

Uploaded by

shubhrajkumar707
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

6-7-Adding two polynomials using Linked List

The document discusses how to add two polynomials represented by linked lists, detailing both recursive and iterative approaches. It provides examples of polynomial addition and outlines the time and space complexities for each method. The recursive method has a time complexity of O(m+n) and space complexity of O(max(m,n)), while the iterative method also has a time complexity of O(m+n) but uses O(1) space.

Uploaded by

shubhrajkumar707
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Adding two polynomials using Linked List

Last Updated : 13 Sep, 2024

Given two polynomial numbers represented by a linked list. The task is to add these lists meaning the
coefficients with the same variable powers will be added.
Note: Given polynomials are sorted in decreasing order of power.

Example:

Input:
head1: [[5, 2], [4, 1], [2, 0]]
head2: [[5, 1], [5, 0]]
Output: [[5, 2], [9, 1], [7, 0]]
Explanation:

Input:
head1: [[5, 3], [4, 2], [2, 0]]
head2: [[5, 1], [-5, 0]]
Output: [[5, 3], [4, 2], [5, 1], [-3, 0]]
Explanation: head1 can be represented as 5x^3 + 4x^2 + 2 , head2 can be represented as 5x –
5, add both the polynomials to get 5x^3 + 4x^2 + 5x – 3

Try it on GfG
Practice
Table of Content
[Expected Approach – 1] Using Recursion – O(n+m) Time and O(max(m,n)) Space
[Expected Approach] Using Iterative Method – O(n+m) Time and O(1) Space

[Expected Approach – 1] Using Recursion – O(m+n) Time and O(max(m,n)) Space:

The idea is to recursively check the heads of both lists. If one of the heads is NULL, then return
the other head. Otherwise, compare the power of both nodes. If the power of one list is greater
than the other, then recursively find the next node of the greater power list. Otherwise, store the
sum of coefficients in one list, and return its head.

Below is the implementation of the above approach:

C++ C Java Python C# JavaScript

// C++ program to add two polynomials


#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int coeff;
int pow;
Node* next;
Node(int c, int p) {
coeff = c;
pow = p;
next = nullptr;
}
};

Node* addPolynomial(Node* head1, Node* head2) {

// if any list is empty, then return


// the other list.
if (head1 == nullptr) return head2;
if (head2 == nullptr) return head1;

// If head1.pow is greater, then recursively find


// its next node, and return head1.
if (head1->pow > head2->pow) {
Node* nextPtr = addPolynomial(head1->next, head2);
head1->next = nextPtr;
return head1;
}

// If head2.pow is greater, then recusrively find its


// next node, and return head2.
else if (head1->pow < head2->pow) {
Node* nextPtr = addPolynomial(head1, head2->next);
head2->next = nextPtr;
return head2;
}

// else store the sum of head1.coeff and head2.coeff in


// head1->coeff, then find its next node and return head1.
Node* nextPtr = addPolynomial(head1->next, head2->next);
head1->coeff += head2->coeff;
head1->next = nextPtr;
return head1;
}

void printList(Node* head) {


Node* curr = head;

while (curr != nullptr) {


cout << curr->coeff << "," << curr->pow <<" ";
curr = curr->next;
}

cout<<endl;
}

int main() {

// 1st polynomial: 5x^2+4x^1+2x^0


Node* head1 = new Node(5,2);
head1->next = new Node(4,1);
head1->next->next = new Node(2,0);

// 2nd polynomial: -5x^1-5x^0


Node* head2 = new Node(-5,1);
head2->next = new Node(-5,0);

Node* head = addPolynomial(head1, head2);

printList(head);
}
Output

5,2 -1,1 -3,0

Time Complexity: O(m+n), where m and n are the number of nodes in both the lists.
Auxiliary Space: O(max(m,n))

[Expected Approach] Using Iterative Method – O(m+n) Time and O(1) Space:

The idea is to create a dummy node which will act as the head of resultant list. Start traversing
both the lists, if list1->pow if not equal to list2->pow, then Link the node with greater power to
the resultant list. Otherwise, add the sum of list1->coeff + list2->coeff to the resultant list.

Below is the implementation of the above approach:

C++ C Java Python C# JavaScript

// C++ program to add two polynomials


#include <bits/stdc++.h>
using namespace std;

class Node {
public:
int coeff;
int pow;
Node* next;
Node(int c, int p) {
coeff = c;
pow = p;
next = nullptr;
}
};

Node* addPolynomial(Node* head1, Node* head2) {


Node* dummy = new Node(0, 0);

// Node to append other nodes to the end


// of list
Node* prev = dummy;

Node* curr1 = head1, *curr2 = head2;

while (curr1 != nullptr && curr2 != nullptr) {

// if curr2.pow > curr1.pow, then


// append curr2 to list
if (curr1->pow < curr2->pow) {
prev->next = curr2;
prev = curr2;
curr2 = curr2->next;
}

// if curr1.pow > curr2.pow, then


// append curr2 to list
else if (curr1->pow > curr2->pow) {
prev->next = curr1;
prev = curr1;
curr1 = curr1->next;
}
// else, add the sum of curr1->coeff and
// curr2->coeff to curr1->coeff, and append
// curr1 to the list
else {
curr1->coeff = curr1->coeff + curr2->coeff;
prev->next = curr1;
prev = curr1;
curr1 = curr1->next;
curr2 = curr2->next;
}
}

// if curr1 if not null, then append the rest


// to the list
if (curr1 != nullptr) {
prev->next = curr1;
}

// if curr2 if not null, then append the rest


// to the list
if (curr2 != NULL) {
prev->next = curr2;
}

return dummy->next;
}

void printList(Node* head) {


Node* curr = head;

while (curr != nullptr) {


cout << curr->coeff << "," << curr->pow << " ";
curr = curr->next;

cout<<endl;
}

int main() {

// 1st polynomial: 5x^2+4x^1+2x^0


Node* head1 = new Node(5,2);
head1->next = new Node(4,1);
head1->next->next = new Node(2,0);

// 2nd polynomial: -5x^1-5x^0


Node* head2 = new Node(-5,1);
head2->next = new Node(-5,0);

Node* head = addPolynomial(head1, head2);

printList(head);
}

Output

5,2 -1,1 -3,0

Time Complexity: O(m + n) where m and n are number of nodes in first and second lists respectively.
Auxiliary Space: O(1)

You might also like