Skip to content

Commit c99b2be

Browse files
committed
follow-up for 2.5_sum_lists.py
1 parent ae253fa commit c99b2be

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

Cracking_the_coding_interview/v6/2.5_sum_lists.py

+38-25
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ def sum_lists_1(ll1, ll2):
4040
# Space: O(Max(M, N))
4141
def sum_lists_2(ll1, ll2):
4242
head = sum_lists_2_helper(ll1.head, ll2.head, 0)
43-
ll = LL([])
44-
ll.head = head
45-
return ll
43+
return LL(head)
4644

4745
def sum_lists_2_helper(node1, node2, carry):
4846
if node1 == None and node2 == None and carry == 0:
@@ -64,17 +62,21 @@ def sum_lists_2_helper(node1, node2, carry):
6462
return Node(value, next)
6563

6664
# assume ll1 and ll2 has at least 1 node
65+
# Time: O(M + N)
66+
# Space: O(Max(M, N))
6767
def sum_lists_follow_up(ll1, ll2):
68+
# modify two lists to be the same length
6869
len1, len2 = ll1.length(), ll2.length()
6970
head1, head2 = ll1.head, ll2.head
7071
if len1 < len2:
7172
head1 = append_zero_nodes(head1, len2 - len1)
7273
elif len1 > len2:
7374
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)
7880

7981
# input: n must be positive integer
8082
def append_zero_nodes(node, num):
@@ -85,8 +87,18 @@ def append_zero_nodes(node, num):
8587
pt.next = node
8688
return head
8789

90+
# two lists should be the same length
8891
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)
90102

91103
class Test(UnitTestBase, unittest.TestCase):
92104
def data_provider(self):
@@ -108,23 +120,24 @@ def func_eval(self, func, args):
108120
ll1, ll2 = args
109121
return func(ll1, ll2)
110122

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)
128141

129142
if __name__ == '__main__':
130143
unittest.main()
Binary file not shown.

0 commit comments

Comments
 (0)