-
Notifications
You must be signed in to change notification settings - Fork 6
/
445_Add Two Numbers II.py
72 lines (64 loc) · 1.76 KB
/
445_Add Two Numbers II.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 16 15:36:38 2020
@author: leiya
"""
'''
0701:相当于基于stack的add binary
'''
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
pre = None
carry = 0
stack1 = []
stack2 = []
while l1:
stack1.append(l1.val)
l1 = l1.next
while l2:
stack2.append(l2.val)
l2 = l2.next
while stack1 or stack2 or carry:
if stack1:
temp1 = stack1.pop()
else:
temp1 = 0
if stack2:
temp2 = stack2.pop()
else:
temp2 = 0
cur_val = temp1 + temp2 + carry
cur_use = cur_val % 10
carry = cur_val // 10
cur_node = ListNode(cur_use)
cur_node.next = pre
pre = cur_node
return pre
#双栈问题
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode):
dummynode = ListNode(0)
stack1, stack2 = [], []
carry = 0
while l1 is not None:
stack1.append(l1.val)
l1 = l1.next
while l2 is not None:
stack2.append(l2.val)
l2 = l2.next
while stack1 or stack2 or carry:
if stack1:
val1 = stack1.pop()
else:
val1 = 0
if stack2:
val2 = stack2.pop()
else:
val2 = 0
value = val1 + val2 + carry
valnode = value % 10
carry = value // 10
node = ListNode(valnode)
node.next = dummynode.next
dummynode.next = node
return dummynode.next