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.
Input: 1st number = 5x^2 + 4x^1 + 2x^0 2nd number = 5x^1 + 5x^0 Output: 5x^2 + 9x^1 + 7x^0 Input: 1st number = 5x^3 + 4x^2 + 2x^0 2nd number = 5x^1 + 5x^0 Output: 5x^3 + 4x^2 + 5x^1 + 7x^0
1st Number: 5x^2 + 4x^1 + 2x^0 2nd Number: 5x^1 + 5x^0 Added polynomial: 5x^2 + 9x^1 + 7x^0
Time Complexity: O(m + n) where m and n are number of nodes in first and second lists respectively.
Related Article: Add two polynomial numbers using Arrays
This article is contributed by Akash Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Move all occurrences of an element to end in a linked list
- Implementing Iterator pattern of a single Linked List
- Circular Singly Linked List | Insertion
- Length of longest palindrome list in a linked list using O(1) extra space
- Clone a linked list with next and random pointer in O(1) space
- Program to add two polynomials
- Multiply two polynomials
- Delete all occurrences of a given key in a linked list
- Circular Linked List | Set 1 (Introduction and Applications)
- Add two numbers represented by linked lists | Set 2
- Detect and Remove Loop in a Linked List
- Reverse a Doubly Linked List
- Function to check if a singly linked list is palindrome
- Reverse a linked list
- Write a program to calculate pow(x,n)