Accepted Python solution. Really basic implementation


  • 3
    L
    class Solution:
        # @return a ListNode
        def addTwoNumbers(self, l1, l2):
            carry = 0
            sum = ListNode(0)
            s = sum
            while l1 is not None or l2 is not None or carry:
                s.val = carry
                if l1:
                    s.val += l1.val
                    l1 = l1.next
                if l2:
                    s.val += l2.val
                    l2 = l2.next
                carry = s.val / 10
                s.val = s.val % 10
                if l1 or l2 or carry:
                    s.next = ListNode(0)
                    s = s.next
            return sum
    

    Even though on the LeetCode runtime graph, the algo seems to achieve CPP level runtimes, I am a relatively new to Python and I appreciate any suggestions to make the code more concise.
    enter image description here


  • 0
    C
    class Solution:
    # @return a ListNode
    
    def addTwoNumbers(self, l1, l2):
        
        result = ListNode(0)
        carry = 0
        last = temp = result
        while l1 or l2 or carry:
            sum = 0
            if l1:
                sum += l1.val
                l1 = l1.next
            
            if l2:
                sum += l2.val
                l2 = l2.next
            
            if not temp:
                temp = ListNode(0)
                last.next = temp
            
            sum += carry
            temp.val = sum % 10
            carry = sum / 10
    
            last = temp
            temp = temp.next
       
        return result
    

    Thx for your sharing, here is my code, almost as same as your version, but a little faster

    Runtime: 203 ms


  • 0
    F

    Nice solution, runtime is probably due to the recent leetcode server upgrade.


  • 0
    H
    class Solution:
    # @return a ListNode
    def addTwoNumbers(self, l1, l2):
        p1,p2 = l1,l2
        l, pp = None, None
        is_carried = 0
        while p1 is not None or p2 is not None:
            if p1 is None:
                sum = p2.val + is_carried
                p2 = p2.next
            elif p2 is None:
                sum = p1.val + is_carried
                p1 = p1.next
            else:
                sum = p1.val + p2.val + is_carried
                p1 = p1.next
                p2 = p2.next
            if sum > 9 :
                is_carried = 1
            else:
                is_carried = 0
            p = ListNode(sum % 10)
            if l is None:
                l = p
                pp = l
            else:
                pp.next = p
                pp = p
        
        if is_carried:
            pp.next = ListNode(is_carried)
            
        return l
    

    here is my code.it takes 146ms


  • 0
    Y

    Elegant and easy readable solution. Only suggestion is if you can define your is_carried to be "sum/10" instead of "1", so that if the question is changed to more than one digit, the code is still valid.


Log in to reply
 

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