工程之美
什么样的工程是美的:
- 可维护
- 可读性
- 可扩展性
如果链表的长度不是特别恐怖的情况下, 这个栈的方案是非常有价值的.
易懂是第一点, 有了问题也能及时排查到.
使用栈来解决问题
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
n1 = []
n2 = []
while l1:
n1.append(l1.val)
l1 = l1.next
while l2:
n2.append(l2.val)
l2 = l2.next
count = 0
result = []
while n1 or n2 or count:
a = n1.pop() if n1 else 0
b = n2.pop() if n2 else 0
sum = a + b + count
if sum<10:
result.append(sum)
count = 0
else:
result.append(sum-10)
count = 1
res = ListNode(0)
r = res
for i in result[::-1]:
r.next = ListNode(i)
r = r.next
return res.next