-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
445 Add Two Numbers II.swift
108 lines (88 loc) 路 2.37 KB
/
445 Add Two Numbers II.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
// 445 Add Two Numbers II.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 07/11/2020.
// Copyright 漏 2020 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/add-two-numbers-ii/
class Solution {
/// Adds two numbers represented in linked lists.
///
/// - Parameters:
/// - l1: First number represented in the linked list.
/// - l2: Second number represented in the linked list.
/// - Returns: Sum represented in the linked list.
///
/// - Complexity:
/// - time: O(n + m), where n is the length of `l1`, and m is the length of `l2`.
/// - space: O(max(n, m)), where n is the length of `l1`, and m is the length of `l2`.
func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
var n1 = 0
var n2 = 0
var cur1 = l1
var cur2 = l2
while cur1 != nil {
cur1 = cur1?.next
n1 += 1
}
while cur2 != nil {
cur2 = cur2?.next
n2 += 1
}
cur1 = l1
cur2 = l2
var head: ListNode?
while n1 > 0, n2 > 0 {
var val = 0
if n1 >= n2 {
val += cur1?.val ?? 0
cur1 = cur1?.next
n1 -= 1
}
if n1 < n2 {
val += cur2?.val ?? 0
cur2 = cur2?.next
n2 -= 1
}
let newHead = ListNode(val)
newHead.next = head
head = newHead
}
cur1 = head
head = nil
var carry = 0
while let cur = cur1 {
let val = (cur.val + carry) % 10
carry = (cur.val + carry) / 10
let newHead = ListNode(val)
newHead.next = head
head = newHead
cur1 = cur.next
}
if carry != 0 {
let newHead = ListNode(carry)
newHead.next = head
head = newHead
}
return head
}
}
/// Provided code
public class ListNode {
public var val: Int
public var next: ListNode?
public init() {
self.val = 0
self.next = nil
}
public init(_ val: Int) {
self.val = val
self.next = nil
}
public init(_ val: Int, _ next: ListNode?) {
self.val = val
self.next = next
}
}