Accepted python solution


  • 0
    W

    106 ms

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def addTwoNumbers(self, l1, l2):
            """
            :type l1: ListNode
            :type l2: ListNode
            :rtype: ListNode
            """
            result = l3 = ListNode(None)
            k = 0
            while l1 or l2:
                l3.next = ListNode(None)
                l3 = l3.next
                if not l1:
                    l1 = ListNode(0)
                if not l2:
                    l2 = ListNode(0)
                k = l1.val + l2.val + k
                l3.val = k % 10
                k /= 10
                l1 = l1.next
                l2 = l2.next
            if k > 0:
                l3.next = ListNode(k)
            return result.next
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.