Add Two Polynomials Represented as Linked Lists - Problem

A polynomial linked list is a special type of linked list where every node represents a term in a polynomial expression.

Each node has three attributes:

  • coefficient: an integer representing the number multiplier of the term. The coefficient of the term 9x⁴ is 9.
  • power: an integer representing the exponent. The power of the term 9x⁴ is 4.
  • next: a pointer to the next node in the list, or null if it is the last node of the list.

For example, the polynomial 5x³ + 4x - 7 is represented by the polynomial linked list with nodes [[5,3],[4,1],[-7,0]].

The polynomial linked list must be in its standard form:

  • The polynomial must be in strictly descending order by its power value.
  • Terms with a coefficient of 0 are omitted.

Given two polynomial linked list heads, poly1 and poly2, add the polynomials together and return the head of the sum of the polynomials.

PolyNode format: The input/output format is as a list of n nodes, where each node is represented as its [coefficient, power]. For example, the polynomial 5x³ + 4x - 7 would be represented as: [[5,3],[4,1],[-7,0]].

Input & Output

Example 1 — Basic Addition
$ Input: poly1 = [[1,2],[2,1]], poly2 = [[6,2],[4,1]]
Output: [[7,2],[6,1]]
💡 Note: Both polynomials have terms with powers 2 and 1. For power 2: 1 + 6 = 7. For power 1: 2 + 4 = 6. Result is 7x² + 6x.
Example 2 — Different Powers
$ Input: poly1 = [[1,3]], poly2 = [[1,2],[1,1]]
Output: [[1,3],[1,2],[1,1]]
💡 Note: No common powers, so all terms appear in the result: x³ + x² + x.
Example 3 — Coefficients Cancel Out
$ Input: poly1 = [[2,2],[3,1]], poly2 = [[-2,2],[1,0]]
Output: [[3,1],[1,0]]
💡 Note: Power 2 terms cancel: 2 + (-2) = 0, so only 3x + 1 remains.

Constraints

  • 0 ≤ m, n ≤ 104
  • -109 ≤ poly1[i].coefficient, poly2[i].coefficient ≤ 109
  • poly1[i].power > poly1[i+1].power
  • poly2[i].power > poly2[i+1].power

Visualization

Tap to expand
Add Two Polynomials as Linked Lists INPUT poly1: 1x² + 2x coef: 1 pow: 2 coef: 2 pow: 1 NULL poly2: 6x² + 4x coef: 6 pow: 2 coef: 4 pow: 1 NULL Input Arrays: poly1 = [[1,2],[2,1]] poly2 = [[6,2],[4,1]] [coefficient, power] ALGORITHM STEPS 1 Initialize Create dummy head node 2 Compare Powers Traverse both lists Power Comparison: pow=2: 1 + 6 = 7 pow=1: 2 + 4 = 6 3 Add Coefficients Same power: sum coefs 4 Build Result Create new node if coefficient != 0 Time: O(n+m) | Space: O(n+m) FINAL RESULT Sum: 7x² + 6x coef: 7 pow: 2 coef: 6 pow: 1 NULL Computation Detail: x² term: 1 + 6 = 7 x¹ term: 2 + 4 = 6 Descending order maintained Output: [[7,2],[6,1]] OK - Valid Result Key Insight: Use two-pointer technique similar to merging sorted lists. Compare powers at each step: - Equal powers: add coefficients, skip if sum is 0. Different powers: take larger power node. - This maintains descending order and handles all terms in O(n+m) time with single traversal. TutorialsPoint - Add Two Polynomials Represented as Linked Lists | Optimal Solution
Asked in
Google 15 Amazon 12 Microsoft 8
28.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen