Java Program For Adding Two Polynomials Using Linked List Last Updated : 23 Jul, 2025 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 info K kartik Follow Improve Article Tags : Linked List Mathematical Java Programs DSA Amazon maths-polynomial Linked-List-Polynomial +3 More Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 2 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 14 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 6 min read Problem of The Day - Develop the Habit of Coding 5 min read Like