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
}
}