Be care when Using python


  • 0
    R

    Be care, everyone who use python to solve this prob !
    Do not define your own class to describe the List Node, otherwise, U will get RE!
    Keep it simple, just as a simple follow:

    {

    class Solution:

    def addTwoNumbers(self, l1, l2):
        d = 0
        LNode = ListNode(0)
        head = LNode
        
        while d or l1 or l2:
            tmp = ListNode(d)
            if l1:
                tmp.val += l1.val
                l1 = l1.next
            if l2:
                tmp.val += l2.val
                l2 = l2.next
            
            d = tmp.val // 10
            tmp.val %= 10
            
            head.next, head = tmp, tmp
        
        return LNode.next
    

    }


Log in to reply
 

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