Java Program For Adding Two Polynomials Using Linked List Last Updated : 04 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Given two polynomial numbers represented by a linked list. Write a function that add these lists means add the coefficients who have same variable powers.Example: Input: 1st number = 5x2 + 4x1 + 2x0 2nd number = -5x1 - 5x0 Output: 5x2-1x1-3x0 Input: 1st number = 5x3 + 4x2 + 2x0 2nd number = 5x^1 - 5x^0 Output: 5x3 + 4x2 + 5x1 - 3x0 Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Java import java.io.*; import java.util.Scanner; class Polynomial { public static Node addPolynomial(Node p1, Node p2) { Node a = p1, b = p2, newHead = new Node(0, 0), c = newHead; while (a != null || b != null) { if (a == null) { c.next = b; break; } else if (b == null) { c.next = a; break; } else if (a.pow == b.pow) { c.next = new Node(a.coeff + b.coeff, a.pow); a = a.next; b = b.next; } else if (a.pow > b.pow) { c.next = new Node(a.coeff, a.pow); a = a.next; } else if (a.pow < b.pow) { c.next = new Node(b.coeff, b.pow); b = b.next; } c = c.next; } return newHead.next; } } // Utilities for Linked List // Nodes class Node { int coeff; int pow; Node next; Node(int a, int b) { coeff = a; pow = b; next = null; } } // Linked List main class class LinkedList { public static void main(String args[]) { Node start1 = null, cur1 = null, start2 = null, cur2 = null; int[] list1_coeff = {5, 4, 2}; int[] list1_pow = {2, 1, 0}; int n = list1_coeff.length; int i = 0; while (n-- > 0) { int a = list1_coeff[i]; int b = list1_pow[i]; Node ptr = new Node(a, b); if (start1 == null) { start1 = ptr; cur1 = ptr; } else { cur1.next = ptr; cur1 = ptr; } i++; } int[] list2_coeff = {-5, -5}; int[] list2_pow = {1, 0}; n = list2_coeff.length; i = 0; while (n-- > 0) { int a = list2_coeff[i]; int b = list2_pow[i]; Node ptr = new Node(a, b); if (start2 == null) { start2 = ptr; cur2 = ptr; } else { cur2.next = ptr; cur2 = ptr; } i++; } Polynomial obj = new Polynomial(); Node sum = obj.addPolynomial(start1, start2); Node trav = sum; while (trav != null) { System.out.print(trav.coeff + "x^" + trav.pow); if (trav.next != null) System.out.print(" + "); trav = trav.next; } System.out.println(); } } Output: 1st Number: 5x^2+4x^1+2x^0 2nd Number: -5x^1-5x^0 Added polynomial: 5x^2-1x^1-3x^0 Time Complexity: O(m + n) where m and n are number of nodes in first and second lists respectively.Auxiliary Space: O(max(m,n) as it is using extra space for resultant linked list. Please refer complete article on Adding two polynomials using Linked List for more details! Comment More infoAdvertise with us Next Article Java Program For Adding Two Polynomials Using Linked List kartik Follow Improve Article Tags : Linked List Mathematical Java Programs DSA Amazon maths-polynomial Linked-List-Polynomial +3 More Practice Tags : AmazonLinked ListMathematical Similar Reads Java Program For Adding Two Numbers Represented By Linked Lists- Set 2 Given two numbers represented by two linked lists, write a function that returns the sum list. The sum list is linked list representation of the addition of two input numbers. It is not allowed to modify the lists. Also, not allowed to use explicit extra space (Hint: Use Recursion). Example : Input: 8 min read Java Program For Adding 1 To A Number Represented As Linked List Number is represented in linked list such that each digit corresponds to a node in linked list. Add 1 to it. For example 1999 is represented as (1-> 9-> 9 -> 9) and adding 1 to it should change it to (2->0->0->0) Recommended: Please solve it on "PRACTICE" first, before moving on to 6 min read Java Program to Implement Unrolled Linked List An Unrolled Linked List is a special type of Linked List in which each node stores an array of elements, unlike a simple linked list. Here we use an ArrayList and a constructor that initializes the size of the Unrolled Linked List. Elements are added to the first Node until it is filled and then a n 5 min read Java Program For Finding Intersection Of Two Sorted Linked Lists Given two lists sorted in increasing order, create and return a new list representing the intersection of the two lists. The new list should be made with its own memory â the original lists should not be changed. Example: Input: First linked list: 1->2->3->4->6 Second linked list be 2- 5 min read Java Program to Implement Triply Linked List Unlike arrays, linked list elements are not stored at a contiguous location; the elements are linked using pointers. In this post, methods to insert a new node in a linked list are discussed. A node can be inserted in three ways either at the front of the linked list or after a given node or at the 8 min read Java Program For Inserting A Node In A Linked List We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.All programs discussed in this post consider the following representations of the linked list. Java // Linked List Class class LinkedList { // Head of list Node 7 min read Java Program to Implement LinkedList API Linked List is a part of the Collection framework That is present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure in which the elements are not stored in contiguous locations and every element is a separate object with a data pa 10 min read Java Program To Add Two Numbers Represented By Linked Lists- Set 1 Given two numbers represented by two lists, write a function that returns the sum list. The sum list is a list representation of the addition of two input numbers. Example: Input:Â List1: 5->6->3 // represents number 563Â List2: 8->4->2 // represents number 842Â Output:Â Resultant list: 1->4->0->5 // re 4 min read Java Program To Perform Union of Two Linked Lists Using Priority Queue Given two linked lists, your task is to complete the function make union(), which returns the union of two linked lists. This union should include all the distinct elements only. The new list formed should be in non-decreasing order. Input: L1 = 9->6->4->2->3->8 L2 = 1->2->8- 4 min read Java Program to Merge Two Sorted Linked Lists in New List We are given two sorted List and our goal is to merge these two lists into a new list. For that, we have to write one function which will take two List as an argument which is sorted in increasing order. This function will Merge these two List into one List in increasing order. Input List 1 : 1-> 4 min read Like