Similar Problems

Similar Problems not available

Add Two Polynomials Represented As Linked Lists - Leetcode Solution

Companies:

LeetCode:  Add Two Polynomials Represented As Linked Lists Leetcode Solution

Difficulty: Medium

Topics: math linked-list two-pointers  

Problem Statement:

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example 1: Input: l1 = [2,4,3], l2 = [5,6,4] Output: [7,0,8] Explanation: 342 + 465 = 807.

Example 2: Input: l1 = [0], l2 = [0] Output: [0]

Example 3: Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] Output: [8,9,9,9,0,0,0,1]

Solution:

To start with the solution, we need to know what polynomials are.

In mathematics, a polynomial is an expression consisting of variables (also called indeterminates) and coefficients, that involves only the operations of addition, subtraction, multiplication, and non-negative integer exponents of variables.

The polynomial equation is typically written in the form p(x) = a0 + a1x + a2x^2 + ... + anx^n.

To add two polynomials L1 and L2, we need to add their coefficients and form a new polynomial L3 with the resultant coefficients.

Now we shall look into the implementation of the problem through stepwise:

Step 1: Create a function to add the nodes of two linked lists

def add_node(n1, n2, carry): """ Add nodes of two linked lists and return the resultant node. n1 - First node n2 - Second node carry - carry value i.e. value to be added in next addition """ val = carry + (n1.val if n1 else 0) + (n2.val if n2 else 0) # Adding the values from n1 and n2

# Set carry to 1 if val > 9 else 0, while val % 10 gives the unit digit value
carry = 1 if val > 9 else 0
node = ListNode(val % 10) # Creating a new node with modulo 10 value of val

return node, carry

Step 2: Create the function to add the polynomials

def add_two_numbers(l1,l2): # Creating an initial node for the resultant linked list sentinel = current = ListNode(0) carry = 0 # Variable to hold the carry from previous addition

while n1 or n2 or carry:
    # Adding nodes and carry from previous iteration to node for L3
    node, carry = self.add_node(n1, n2, carry)
    current.next = node
    current = current.next # Moving on to next node in the resultant linked list
    n1 = n1.next if n1 else None # Moving on to next node in linked list L1 if n1 exists
    n2 = n2.next if n2 else None # Moving on to next node in linked list L2 if n2 exists

return sentinel.next # Returning the root node of the resultant linked list

Step 3: If we check condition l1 and l2 are None, then we can return None

if not l1 and not l2: return None

Step 4: We will return any non-empty list among l1 and l2

if not l1 or not l2: return l1 or l2

Step 5: Now we call the function add_two_numbers()

l3 = add_two_numbers(l1, l2)

Step 6: Return the resultant linked list

return l3

The complete Python code for the problem is given below:

def add_node(n1, n2, carry): """ Add nodes of two linked lists and return the resultant node. n1 - First node n2 - Second node carry - carry value i.e. value to be added in next addition """ val = carry + (n1.val if n1 else 0) + (n2.val if n2 else 0) # Adding the values from n1 and n2

# Set carry to 1 if val > 9 else 0, while val % 10 gives the unit digit value
carry = 1 if val > 9 else 0
node = ListNode(val % 10) # Creating a new node with modulo 10 value of val

return node, carry

def add_two_numbers(l1,l2): # Creating an initial node for the resultant linked list sentinel = current = ListNode(0) carry = 0 # Variable to hold the carry from previous addition

while l1 or l2 or carry:
    # Adding nodes and carry from previous iteration to node for L3
    node, carry = add_node(l1, l2, carry)
    current.next = node
    current = current.next # Moving on to next node in the resultant linked list
    l1 = l1.next if l1 else None # Moving on to next node in linked list L1 if n1 exists
    l2 = l2.next if l2 else None # Moving on to next node in linked list L2 if n2 exists

return sentinel.next # Returning the root node of the resultant linked list

Main function

if name == "main": # Creating L1 l1 = ListNode(2) l1.next = ListNode(4) l1.next.next = ListNode(3)

# Creating L2
l2 = ListNode(5)
l2.next = ListNode(6)
l2.next.next = ListNode(4)

# Adding two linked list
l3 = add_two_numbers(l1, l2)

# Printing final result
while l3:
    print(l3.val, end=" ")
    l3 = l3.next

Output: 7 0 8

Add Two Polynomials Represented As Linked Lists Solution Code

1