Add Two Numbers Linked List Python – LeetCode Solutions

LeetCode has a coding Problem in Its’ Algorithm Section which is Add Two Numbers Linked List Python. Today We are going to solve this problem. LeetCode Link of the Problem is HERE.

Question

You are given two non-empty linked lists representing two non-negative integers. 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.

add two numbers linked list python
Linked List Addition

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

Conditions

  • The number of nodes in each linked list is in the range [1, 100].
  • No leading Zeros in Linked List
  • 0 <= Node.val <= 9

Solution to Add Two Numbers Linked List Python

The Skeleton Code Given By LeetCode is:

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """

Firstly I created two Variables to store the two input linked Lists as a string. To do so I Looped through the linked list and store the variables as a string. Reversed the String for Addition.

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        a1 = ""
        a2 = ""
        while l1.next != None:
            a1 += str(l1.val)
            l1 = l1.next
        while l2.next != None:
            a2 += str(l2.val)
            l2 = l2.next
        a1 += str(l1.val)
        a2 += str(l2.val)
        a1 = a1[::-1]
        a2 = a2[::-1]

Added the Two Variables as Integers. Created a New Linked List Node and Stored the Integers as Nodes of Linked List.

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        a1 = ""
        a2 = ""
        while l1.next != None:
            a1 += str(l1.val)
            l1 = l1.next
        while l2.next != None:
            a2 += str(l2.val)
            l2 = l2.next
        a1 += str(l1.val)
        a2 += str(l2.val)
        a1 = a1[::-1]
        a2 = a2[::-1]
        a3 = int(a1) + int(a2)
        a3 = list(str(a3))  
        x = ListNode()
        for i in range(0,len(a3)):
            if i == len(a3)-1:
                x.val = a3[i]
            else:
                temp = ListNode()
                temp.val = a3[i]
                temp.next = x.next
                x.next = temp
        return x

READ MORE

Python-related posts Visit HERE

C++ related posts Visit HERE

Databases related posts Visit HERE

Data Structures related posts visit HERE

Algorithms related posts visit HERE

Data Science related posts visit HERE

Share the Knowledge