LeetCode 1634: Add Two Polynomials Represented as Linked Lists Solution

Master LeetCode problem 1634 (Add Two Polynomials Represented as Linked Lists), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1634. Add Two Polynomials Represented as Linked Lists

Problem Explanation

Explanation

To add two polynomials represented as linked lists, we can traverse both linked lists simultaneously, adding the coefficients of corresponding terms and creating a new linked list to store the result. We will merge terms with the same power and remove any terms with a coefficient of 0.

Algorithm:

  1. Initialize three pointers, ptr1 and ptr2 to the heads of the two input linked lists, and dummyHead to the head of the result linked list.
  2. Traverse both input linked lists simultaneously.
  3. While both lists have nodes remaining:
    • If the powers of the current nodes are equal, add the coefficients and create a new node in the result list with the sum.
    • If one list has a higher power, copy the node to the result list.
  4. Attach any remaining nodes from both input lists to the result list.
  5. Return the head of the result linked list.

Time Complexity: O(max(m, n)), where m and n are the number of terms in the input polynomials. Space Complexity: O(max(m, n)) for the result linked list.

Solution Code

class ListNode {
    int coefficient;
    int power;
    ListNode next;

    public ListNode(int coefficient, int power) {
        this.coefficient = coefficient;
        this.power = power;
    }
}

public ListNode addTwoPolynomials(ListNode poly1, ListNode poly2) {
    ListNode dummyHead = new ListNode(0, 0);
    ListNode current = dummyHead;

    while (poly1 != null && poly2 != null) {
        if (poly1.power == poly2.power) {
            int sum = poly1.coefficient + poly2.coefficient;
            if (sum != 0) {
                current.next = new ListNode(sum, poly1.power);
                current = current.next;
            }
            poly1 = poly1.next;
            poly2 = poly2.next;
        } else if (poly1.power > poly2.power) {
            current.next = new ListNode(poly1.coefficient, poly1.power);
            current = current.next;
            poly1 = poly1.next;
        } else {
            current.next = new ListNode(poly2.coefficient, poly2.power);
            current = current.next;
            poly2 = poly2.next;
        }
    }

    while (poly1 != null) {
        current.next = new ListNode(poly1.coefficient, poly1.power);
        current = current.next;
        poly1 = poly1.next;
    }

    while (poly2 != null) {
        current.next = new ListNode(poly2.coefficient, poly2.power);
        current = current.next;
        poly2 = poly2.next;
    }

    return dummyHead.next;
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1634 (Add Two Polynomials Represented as Linked Lists)?

This page provides optimized solutions for LeetCode problem 1634 (Add Two Polynomials Represented as Linked Lists) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1634 (Add Two Polynomials Represented as Linked Lists)?

The time complexity for LeetCode 1634 (Add Two Polynomials Represented as Linked Lists) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1634 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1634 in Java, C++, or Python.

Back to LeetCode Solutions