Swift-Add Two Numbers

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.

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

解法一:

  func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
        
        if l1 == nil {
            return l2
        }
        
        if l2 == nil {
            return l1
        }
        
        var headNode:ListNode?
        
        var head1:ListNode? = l1
        var head2:ListNode? = l2
        var carry:Int = 0
        
        while head1 != nil {
            
            let value:Int = (head1?.val)! + (head2?.val)! + carry
            
            let listNode:ListNode? = ListNode(value % 10)
            
            if headNode == nil {
                headNode = listNode
            } else {
                var nextNode:ListNode? = headNode
                while nextNode?.next != nil {
                    nextNode = nextNode?.next
                }
                nextNode?.next = listNode
            }
            carry = value / 10
            head1 = head1?.next
            head2 = head2?.next
        }
        
        return headNode
    }

改進版:

   func addTwoNumbers2(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
        
        let headNode = ListNode(0)
        var listNode = headNode
        
        var head1:ListNode? = l1
        var head2:ListNode? = l2
        var carry:Int = 0
        
        while head1 != nil || head2 != nil || carry != 0 {
            
            var sum:Int = carry
            
            if head1 != nil {
                sum += (head1?.val)!
                head1 = head1?.next
            }
            
            if head2 != nil {
                sum += (head2?.val)!
                head2 = head2?.next
            }
            
            carry = sum / 10
            
            listNode.next = ListNode(sum % 10)
            listNode = listNode.next!
        }
        
        return headNode.next
    }

鏈表定義如下:

public class ListNode {
     public var val: Int
     public var next: ListNode?
     public init(_ val: Int) {
       self.val = val
       self.next = nil
     }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,169評論 0 23
  • Introduction to HTS Flywheel Energy Storage 1. 儲能方式介紹 儲能技...
    iminriver閱讀 2,223評論 0 0
  • 準(zhǔn)備想你的時候 我突然忘了你說過的話 我們沿著南湖大道向西走 往北拐個彎就走到火車站 你向前奔跑 我在你身后追隨 ...
    周瀟灑閱讀 304評論 0 1
  • 謝謝你出現(xiàn)在我的生命里,你讓我變得更堅強,靈魂卻更加柔軟了。 帥帥滿兩個月了,長得胖嘟嘟的,可愛極了。看著他熟睡的...
    知筆行閱讀 413評論 0 0

友情鏈接更多精彩內(nèi)容