0% found this document useful (0 votes)
106 views2 pages

6-9-Multiplication of Two Polynomials Using Linked List

The document discusses the multiplication of two polynomials represented as linked lists, providing examples and an approach for implementation. It explains how to traverse the linked lists, calculate products, and store results in a map to create the resultant polynomial. The document includes a code snippet in C++ demonstrating the multiplication process.

Uploaded by

shubhrajkumar707
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views2 pages

6-9-Multiplication of Two Polynomials Using Linked List

The document discusses the multiplication of two polynomials represented as linked lists, providing examples and an approach for implementation. It explains how to traverse the linked lists, calculate products, and store results in a map to create the resultant polynomial. The document includes a code snippet in C++ demonstrating the multiplication process.

Uploaded by

shubhrajkumar707
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Multiplication of two polynomials using Linked list

Last Updated : 03 Dec, 2024

Given two polynomials in the form of a linked list, the task is to perform the multiplication of both polynomials.

Examples:

Input: poly1: 3x^2 + 5x^1 + 6, poly2: 6x^1 + 8


Output: 18x^3 + 54x^2 + 76x^1 + 48
Explanation: On multiplying each element of 1st polynomial with elements of 2nd polynomial, we get
18x^3 + 24x^2 + 30x^2 + 40x^1 + 36x^1 + 48
On adding values with same power of x,
18x^3 + 54x^2 + 76x^1 + 48

Input: poly1: 3x^3 + 6x^1 – 9, poly2: 9x^3 – 8x^2 + 7x^1 + 2


Output: 27x^6 – 24x^5 + 75x^4 – 123x^3 + 114x^2 – 51x^1 – 18

Approach:

The idea is to traverse the second list for each node in the first list, calculate the product of coefficients
and sum of powers, and store the results in a map where the key represents the power and the value
represents the coefficient. Finally, traverse the map to create new nodes for the resulting polynomial, link
them, and return the head of the resultant list.

C++ Java Python C# JavaScript

1
// C++ Program to multiply two polynomials

2
// represented as linked lists

3
#include <bits/stdc++.h>

4
using namespace std;

6
class Node {

7
public:

8
int coeff, power;
9
Node *next;

10
Node(int p, int c) {

11
power = p;

12
coeff = c;

13
next = nullptr;

14
}

15
};

16

17
Node *multiplyPolynomials(Node *poly1, Node *poly2) {

18

19
map<int, int> resultMap;

20

21
// Multiply each node of poly2 with poly1

22
for (Node *curr1 = poly1; curr1 != nullptr; curr1 = curr1->next) {

23
for (Node *curr2 = poly2; curr2 != nullptr; curr2 = curr2->next) {

24
int power = curr1->power + curr2->power;

25
int coeff = curr1->coeff * curr2->coeff;

26
resultMap[power] += coeff;

27
}

28
}

29

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy