2. 两数相加

作者: cptn3m0 | 来源:发表于2019-03-15 21:09 被阅读0次

    代码缺点

    虽然效率不低, 但是可读性不好, 过于繁琐, 属于笨蛋代码, 需要简化.

    # 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
            """
            
            dummy_head = ListNode(0)
            dummy_list = dummy_head
            if l1 is None and l2 is None:
              return None
            elif l1 is None:
              return l2
            elif l2 is None:
              return l1
            
            carry = 0
            
            while l1 is not None and l2 is not None:
              my_val = l1.val+l2.val+carry
              carry = my_val//10
              my_val = my_val %10
              node = ListNode(my_val)
              l1 = l1.next
              l2 = l2.next
              dummy_list.next = node
              dummy_list = dummy_list.next
              
            while l1 is not None:
              my_val = l1.val+carry
              carry = my_val//10
              my_val = my_val%10
              node = ListNode(my_val)
              l1 = l1.next
              dummy_list.next = node
              dummy_list = dummy_list.next
              
            while l2 is not None:
              my_val = l2.val+carry
              carry = my_val//10
              my_val = my_val%10
              node = ListNode(my_val)
              l2 = l2.next
              dummy_list.next = node
              dummy_list = dummy_list.next
              
              
            if carry > 0:
              node = ListNode(carry)
              dummy_list.next = node
            
            return dummy_head.next
            
    

    相关文章

      网友评论

        本文标题:2. 两数相加

        本文链接:https://www.haomeiwen.com/subject/xthdmqtx.html