Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def add_two_lists(self, list1: list[str], list2: list[str]) -> str:
- from collections import deque
- answers = deque([])
- remainder = 0
- i1 = len(list1) - 1; i2 = len(list2) - 1
- while True:
- # Base
- if i1 < 0 and i2 < 0:
- break
- num1 = int(list1[i1]) if not (i1 < 0) else 0
- num2 = int(list2[i2]) if not (i2 < 0) else 0
- total = num1 + num2 + remainder
- next_digit, remainder = total % 10, total // 10 # max total = 18
- answers.appendleft(str(next_digit))
- i1 -= 1; i2 -= 1
- if remainder > 0:
- answers.appendleft(str(remainder))
- return "".join(answers)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement