美文网首页
002- add two numbers

002- add two numbers

作者: 英武 | 来源:发表于2018-02-11 20:57 被阅读5次

    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 contain a single digit. Add the two numbers and return it as a linked list.

    You may assume the two numbers do not contain any leading zero, except the number 0 itself.

    Example

    Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
    Output: 7 -> 0 -> 8
    Explanation: 342 + 465 = 807.
    

    问题的关键在于,如果某个链表为空,则返回另一个链表。否则就返货两个链表的相应值的和

    # 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
            """
            ret = ListNode(0)
            cur = ret
    
            sum = 0
            while True:
                if l1 != None:
                    sum += l1.val
                    l1 = l1.next
                if l2 != None:
                    sum += l2.val
                    l2 = l2.next
    
                cur.val = sum % 10
                sum /= 10
                if l1 != None or l2 != None or sum != 0:
                    cur.next = ListNode(0)
                    cur = cur.next
                else:
                    break
    
            return ret
    

    相关文章

      网友评论

          本文标题:002- add two numbers

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