Skip to content

Commit ae253fa

Browse files
committed
another solution for 2.5_sum_lists.py
1 parent 6834183 commit ae253fa

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

Cracking_the_coding_interview/v6/2.5_sum_lists.py

+41-5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
# Time: O(Max(M, N))
2020
# Space: O(Max(M, N))
2121
def sum_lists_1(ll1, ll2):
22-
ll = LL([])
23-
2422
cur = head = Node(None) # dummy node
2523
carry, pt1, pt2 = 0, ll1.head, ll2.head
2624
while pt1 != None or pt2 != None or carry != 0:
@@ -35,8 +33,7 @@ def sum_lists_1(ll1, ll2):
3533
cur.next = Node(value)
3634
cur = cur.next
3735

38-
ll.head = head.next
39-
return ll
36+
return LL(head.next)
4037

4138
# Solution: use recursion
4239
# Time: O(Max(M, N))
@@ -66,8 +63,29 @@ def sum_lists_2_helper(node1, node2, carry):
6663
)
6764
return Node(value, next)
6865

69-
66+
# assume ll1 and ll2 has at least 1 node
7067
def sum_lists_follow_up(ll1, ll2):
68+
len1, len2 = ll1.length(), ll2.length()
69+
head1, head2 = ll1.head, ll2.head
70+
if len1 < len2:
71+
head1 = append_zero_nodes(head1, len2 - len1)
72+
elif len1 > len2:
73+
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
78+
79+
# input: n must be positive integer
80+
def append_zero_nodes(node, num):
81+
pt = head = Node(0)
82+
for _ in range(num - 1):
83+
pt.next = Node(0)
84+
pt = pt.next
85+
pt.next = node
86+
return head
87+
88+
def sum_lists_follow_up_helper(node1, node2):
7189
pass
7290

7391
class Test(UnitTestBase, unittest.TestCase):
@@ -90,5 +108,23 @@ def func_eval(self, func, args):
90108
ll1, ll2 = args
91109
return func(ll1, ll2)
92110

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)
128+
93129
if __name__ == '__main__':
94130
unittest.main()

Cracking_the_coding_interview/v6/data_structure.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ def __str__(self):
66
return str(self.data)
77

88
class LinkedList():
9-
def __init__(self, lst = []):
9+
def __init__(self, args = []):
10+
if isinstance(args, Node):
11+
self.head = args
12+
return
13+
1014
cur = None
11-
for i in range(len(lst) - 1, -1, -1):
12-
data = lst[i]
15+
for i in range(len(args) - 1, -1, -1):
16+
data = args[i]
1317
n = Node(data, cur)
1418
cur = n
1519
self.head = cur
@@ -28,3 +32,10 @@ def get(self, index):
2832
node = node.next
2933
return node
3034

35+
def length(self):
36+
length, pt = 0, self.head
37+
while pt != None:
38+
pt = pt.next
39+
length += 1
40+
return length
41+

0 commit comments

Comments
 (0)