forked from hoanhan101/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_two_numbers_test.go
121 lines (98 loc) · 2.44 KB
/
add_two_numbers_test.go
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
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
Problem:
- Given two linked lists representing two non-negative number, add them together
and return it as a linked list.
Assumption:
- The digits are stored in reverse order.
- Each node contains a single digit.
Example:
- Input: (1 -> 6 -> 4) + (2 -> 4-> 1)
Output: (3 -> 0 -> 6)
Approach:
- Traverse both lists and keep track of the sum and carry for each
digit.
Solution:
- Initialize dummy heads to keep track of the head nodes.
- While either list is not empty, calculate the sum of corresponding
digits, update it carry at each step and add it after the current node.
- If the sum have an extra carry at the end, simply make a new node and
add it in the end.
Cost:
- O(n|m) time, O(m|n) space where m and m are lengths of these two lists.
*/
package leetcode
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestAddTwoNumbers(t *testing.T) {
t11 := common.NewListNode(1)
t11.AddNext(6)
t11.AddNext(4)
t12 := common.NewListNode(2)
t12.AddNext(4)
t12.AddNext(1)
t13 := common.NewListNode(3)
t13.AddNext(0)
t13.AddNext(6)
t21 := common.NewListNode(9)
t21.AddNext(9)
t22 := common.NewListNode(1)
t23 := common.NewListNode(0)
t23.AddNext(0)
t23.AddNext(1)
tests := []struct {
in1 *common.ListNode
in2 *common.ListNode
expected *common.ListNode
}{
{t11, t12, t13},
{t21, t22, t23},
}
for _, tt := range tests {
common.Equal(
t,
tt.expected,
addTwoNumbers(tt.in1, tt.in2),
)
}
}
func addTwoNumbers(l1, l2 *common.ListNode) *common.ListNode {
// initialize dummy heads to keep track of the head nodes.
dummyHead := &common.ListNode{}
current := dummyHead
p1 := l1
p2 := l2
carry := 0
// while either list is not empty.
for p1 != nil || p2 != nil {
// if p1 or p2 is nil, use 0 as its value.
p1v, p2v := 0, 0
if p1 != nil {
p1v = p1.Value
}
if p2 != nil {
p2v = p2.Value
}
// calculate the sum of corresponding digits and update it carry.
sum := carry + p1v + p2v
digit := sum % 10
carry = sum / 10
// add a next node the current node.
current.Next = &common.ListNode{Value: digit, Next: nil}
current = current.Next
// keep traversing as long as we have not reached the end.
if p1 != nil {
p1 = p1.Next
}
if p2 != nil {
p2 = p2.Next
}
}
// if the sum have an extra carry at the end, simply make a new node and
// add it in the end.
if carry > 0 {
current.Next = &common.ListNode{Value: carry, Next: nil}
}
return dummyHead.Next
}