-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddTwoNumbersII.py
50 lines (47 loc) · 1.46 KB
/
AddTwoNumbersII.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
'''
iterate through each linked list and read them to stack.
pop two stacks at the same time and calculate the answer, and store it in the result stack
be aware of a corner case that there may be a carry at the end of addition.
pop the result stack and construct the result linked list
'''
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
stack1 = []
stack2 = []
while l1:
stack1.append(l1.val)
l1 = l1.next
while l2:
stack2.append(l2.val)
l2 = l2.next
carry = 0
result = []
while len(stack1) > 0 or len(stack2) > 0:
curr = 0
if len(stack1) > 0:
curr += stack1[-1]
stack1.pop(-1)
if len(stack2) > 0:
curr += stack2[-1]
stack2.pop(-1)
curr += carry
carry = 0
if curr >= 10:
curr -= 10
carry = 1
result.append(curr)
if carry == 1:
result.append(1)
dummy = ListNode(-1)
current = dummy
while len(result) > 0:
current_node = ListNode(result[-1])
result.pop(-1)
current.next = current_node
current = current.next
return dummy.next