@@ -40,9 +40,7 @@ def sum_lists_1(ll1, ll2):
40
40
# Space: O(Max(M, N))
41
41
def sum_lists_2 (ll1 , ll2 ):
42
42
head = sum_lists_2_helper (ll1 .head , ll2 .head , 0 )
43
- ll = LL ([])
44
- ll .head = head
45
- return ll
43
+ return LL (head )
46
44
47
45
def sum_lists_2_helper (node1 , node2 , carry ):
48
46
if node1 == None and node2 == None and carry == 0 :
@@ -64,17 +62,21 @@ def sum_lists_2_helper(node1, node2, carry):
64
62
return Node (value , next )
65
63
66
64
# assume ll1 and ll2 has at least 1 node
65
+ # Time: O(M + N)
66
+ # Space: O(Max(M, N))
67
67
def sum_lists_follow_up (ll1 , ll2 ):
68
+ # modify two lists to be the same length
68
69
len1 , len2 = ll1 .length (), ll2 .length ()
69
70
head1 , head2 = ll1 .head , ll2 .head
70
71
if len1 < len2 :
71
72
head1 = append_zero_nodes (head1 , len2 - len1 )
72
73
elif len1 > len2 :
73
74
head2 = append_zero_nodes (head2 , len1 - len2 )
74
- head = sum_lists_follow_up_helper (head1 , head2 )
75
- ll = LL ([])
76
- ll .head = head
77
- return ll
75
+
76
+ (head , carry ) = sum_lists_follow_up_helper (head1 , head2 )
77
+ if carry != 0 :
78
+ head = Node (carry , head )
79
+ return LL (head )
78
80
79
81
# input: n must be positive integer
80
82
def append_zero_nodes (node , num ):
@@ -85,8 +87,18 @@ def append_zero_nodes(node, num):
85
87
pt .next = node
86
88
return head
87
89
90
+ # two lists should be the same length
88
91
def sum_lists_follow_up_helper (node1 , node2 ):
89
- pass
92
+ if node1 == None :
93
+ return (None , 0 )
94
+
95
+ node , carry = sum_lists_follow_up_helper (node1 .next , node2 .next )
96
+
97
+ tmp = node1 .data + node2 .data + carry
98
+ new_carry , value = tmp // 10 , tmp % 10
99
+ head = Node (value , node )
100
+
101
+ return (head , new_carry )
90
102
91
103
class Test (UnitTestBase , unittest .TestCase ):
92
104
def data_provider (self ):
@@ -108,23 +120,24 @@ def func_eval(self, func, args):
108
120
ll1 , ll2 = args
109
121
return func (ll1 , ll2 )
110
122
111
- # class TestFollowUp(UnitTestBase, unittest.TestCase):
112
- # def data_provider(self):
113
- # return [
114
- # ((LL([1]), LL([1])), LL([2])),
115
- # ((LL([1]), LL([9])), LL([1, 0])),
116
- # ((LL([6, 1, 7]), LL([2, 9, 5])), LL([9, 1, 2])),
117
- # ((LL([1]), LL([9, 9, 9])), LL([1, 0, 0, 0])),
118
- # ]
119
-
120
- # def func_provider(self):
121
- # return [
122
- # sum_lists_follow_up,
123
- # ]
124
-
125
- # def func_eval(self, func, args):
126
- # ll1, ll2 = args
127
- # return func(ll1, ll2)
123
+ class TestFollowUp (UnitTestBase , unittest .TestCase ):
124
+ def data_provider (self ):
125
+ return [
126
+ ((LL ([1 ]), LL ([1 ])), LL ([2 ])),
127
+ ((LL ([1 ]), LL ([9 ])), LL ([1 , 0 ])),
128
+ ((LL ([6 , 1 , 7 ]), LL ([2 , 9 , 5 ])), LL ([9 , 1 , 2 ])),
129
+ ((LL ([1 ]), LL ([9 , 9 , 9 ])), LL ([1 , 0 , 0 , 0 ])),
130
+ ((LL ([8 , 7 , 9 ]), LL ([5 , 8 , 6 ])), LL ([1 , 4 , 6 , 5 ])),
131
+ ]
132
+
133
+ def func_provider (self ):
134
+ return [
135
+ sum_lists_follow_up ,
136
+ ]
137
+
138
+ def func_eval (self , func , args ):
139
+ ll1 , ll2 = args
140
+ return func (ll1 , ll2 )
128
141
129
142
if __name__ == '__main__' :
130
143
unittest .main ()
0 commit comments