![]() |
VOOZH | about |
Given two polynomial numbers represented by a linked list. Write a function to perform their algebraic sum. Examples:
Input: 1st number = 5x^2 + 4x^1 + 2x^0 2nd number = 5x^1 + 5x^0 Output: 5x^2 + 9x^1 + 7x^0
Approach: The implementation uses map data structure so that all coefficients of same power value are added together and kept in key-value pairs inside a map. Below is the implementation of the above approach:
Time Complexity: O((m + n)log(m+n)) where m and n are numbers of nodes in first and second lists respectively and we are using a map for adding the coefficients extra log(m+n) factor is added.