Skip to content

Commit 222cec6

Browse files
committed
update forward soln, reverse input lists, call sum_lists
1 parent 7709655 commit 222cec6

File tree

1 file changed

+18
-89
lines changed

1 file changed

+18
-89
lines changed

Python/chapter02/2.5 - Sum Lists/miguel_2.5_soln.py

Lines changed: 18 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -149,96 +149,25 @@ def sum_lists_forward(ll1: LinkedList, ll2: LinkedList) -> LinkedList:
149149
:param ll2: second input linked list
150150
:return: a linked list containing the result of the addition
151151
"""
152-
shorter_ll, longer_ll = (ll1, ll2) if ll1.size < ll2.size else (ll2, ll1)
153-
n_shorter = shorter_ll.head
154-
n_longer = longer_ll.head
155-
length_diff = abs(shorter_ll.size - longer_ll.size)
156-
# first, advance longer list to line up with
157-
# digit of shorter list
158-
# for example, if we have:
159-
# longer_ll = 9 -> 9 -> 9 -> 9
160-
# shorter_ll = 9 -> 9
161-
# we will line up the current pointer
162-
# of longer_ll to correspond to the
163-
# '10s' digit place because we must add
164-
# digits that are in the same place value.
165-
# n_longer will point to the following parenthesis
166-
# 9 -> 9 -> (9) -> 9
167-
# n_shorter will point to the beginning of shorter_ll
168-
# (9) -> 9
169-
for i in range(length_diff):
170-
n_longer = n_longer.next
152+
n1 = ll1.head
153+
n2 = ll2.head
154+
# reverse both lists
155+
reversed_ll1 = LinkedList()
156+
reversed_ll2 = LinkedList()
157+
while n1 is not None:
158+
reversed_ll1.append_to_head(n1.data)
159+
n1 = n1.next
160+
while n2 is not None:
161+
reversed_ll2.append_to_head(n2.data)
162+
n2 = n2.next
163+
# then, call sum_lists
164+
reversed_result = sum_lists(reversed_ll1, reversed_ll2)
165+
# reverse a final time
171166
output_ll = LinkedList()
172-
# perform addition, taking into account a
173-
# 'backwards' carry value since we
174-
# are looping forward digits down to
175-
# 'ones' digit place
176-
prev_node = None
177-
leading_bw_carry = False
178-
while n_shorter is not None:
179-
first = n_shorter.data
180-
second = n_longer.data
181-
result = first + second
182-
if result >= 10:
183-
backwards_carry = 1
184-
result -= 10 # extract digit in one's place
185-
if prev_node is None:
186-
# for when the first addition yields a carry
187-
# and the output linked list is empty
188-
leading_bw_carry = True
189-
else:
190-
# add carry to previous digit
191-
prev_node.data += backwards_carry
192-
output_ll.append_to_tail(result)
193-
n_shorter = n_shorter.next
194-
n_longer = n_longer.next
195-
prev_node = output_ll.tail
196-
197-
# first case after adding same digit places,
198-
# both input lists same size
199-
if length_diff == 0:
200-
if leading_bw_carry:
201-
backwards_carry = 1
202-
output_ll.append_to_head(backwards_carry)
203-
return output_ll
204-
# otherwise, we have non-equal lengths
205-
# with or without a leading backward carry
206-
# leading bw carry means the the size diff
207-
# decreased.
208-
# no leading bw carry means size diff stayed the same
209-
if leading_bw_carry:
210-
backwards_carry = 1
211-
else:
212-
backwards_carry = 0
213-
# otherwise, as long as the diff is >= 0
214-
# we want to take care of the leading digits.
215-
# start by decrementing the diff
216-
length_diff -= 1
217-
while length_diff >= 0:
218-
# advance the n_longer pointer from longer_ll head
219-
# up to where the current diff is.
220-
n_longer = longer_ll.head
221-
for i in range(length_diff):
222-
n_longer = n_longer.next
223-
224-
if backwards_carry == 1:
225-
output_ll.append_to_head(backwards_carry)
226-
# output data will have the backwards_carry
227-
# appended from before
228-
result = output_ll.head.data + n_longer.data
229-
if result >= 10:
230-
result -= 10
231-
else:
232-
backwards_carry = 0
233-
output_ll.head.data = result
234-
else:
235-
# if backwards carry is 0, bring corresponding digit down
236-
result = n_longer.data
237-
output_ll.append_to_head(result)
238-
length_diff -= 1
239-
# last backwards carry
240-
if backwards_carry == 1:
241-
output_ll.append_to_head(backwards_carry)
167+
n = reversed_result.head
168+
while n is not None:
169+
output_ll.append_to_head(n.data)
170+
n = n.next
242171
return output_ll
243172

244173

0 commit comments

Comments
 (0)