Leetcode 2: Add Two Numbers

題目出處

來自于leetcode

題目描述

You are given two linked lists representing two non-negative numbers. 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.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

解讀

給定兩個表示兩個非負整數的單鏈表,這兩個數按反序保存在單鏈表中,把兩個數相加并返回一個單鏈表。
給定的例子為342 + 465 = 807

第一版答案

  1. 從兩個單鏈表的開頭相加,記住進位;
  2. 幾個邊界條件需要注意:
  • 入參的兩個單鏈表有可能為空,若一個為空,則直接返回另一個即可;
  • 兩個單鏈表長度可能不一樣,在相加結束后需要把比較長的剩余的數值拷貝過去;
  • 進位也需要考慮,如5 + 5 = 10, 當兩個單鏈表處理完畢后,如果還有進位,還需要增加一個節點

代碼如下

# 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
        """
        if not l1:
            return l2
        if not l2:
            return l1
           
        carry = 0
        sums = l1.val + l2.val + carry
        carry = sums / 10
        res = ListNode(sums % 10)
        l1 = l1.next
        l2 = l2.next
       
        node  = res
        while l1 and l2:
            sums = l1.val + l2.val + carry
            tmp = ListNode(sums % 10)
            carry = sums / 10
           
            node.next = tmp
            node = node.next
            l1 = l1.next
            l2 = l2.next
           
        while l1:
            sums = l1.val + carry
            tmp = ListNode(sums % 10)
            carry = sums / 10
           
            node.next = tmp
            node = node.next
            l1 = l1.next
           
        while l2:
            sums = l2.val + carry
            tmp = ListNode(sums % 10)
            carry = sums / 10
           
            node.next = tmp
            node = node.next
            l2 = l2.next
       
        while carry:
            tmp = ListNode(carry % 10)
            carry = carry / 10
           
            node.next = tmp
            node = node.next
           
        return res

第二版答案

第一版答案中的代碼不夠精煉,從以下兩個地方入手:

  1. 入參判斷,對l1和l2的檢查上,先定義一個頭節點,返回頭節點的next,這樣可以避免處理l1或l2為空的情形,將鏈表中所有的節點一視同仁進行處理;
  2. 將四個while語句合并處理

代碼如下:

# 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
        """
        head = ListNode(0)
           
        carry = 0
        node  = head
        while l1 or l2 or carry:
            val1, val2 = 0, 0
            if l1:
                val1 = l1.val
                l1 = l1.next
           
            if l2:
                val2 = l2.val
                l2 = l2.next
               
            sums = val1 + val2 + carry
            tmp = ListNode(sums % 10)
            carry = sums / 10
           
            node.next = tmp
            node = node.next
           
        return head.next

代碼簡潔了許多,由于增加了比較多的比較,效率會稍微下降點。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容