要求: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself.
class Solution:
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
l1_tmp = []
l2_tmp = []
while l1 != None:
l1_tmp.append(l1.val)
l1 = l1.next
while l2 != None:
l2_tmp.append(l2.val)
l2 = l2.next
# print(l1_tmp)
# print(l2_tmp)
if len(l1_tmp) > len(l2_tmp):
for i in range(len(l1_tmp) - len(l2_tmp)):
l2_tmp.append(0)
if len(l1_tmp) < len(l2_tmp):
for i in range(len(l2_tmp) - len(l1_tmp)):
l1_tmp.append(0)
# 初始化
two_sum = []
for i in range(len(l2_tmp)):
two_sum.append(0)
flag = 0
for i in range(len(l2_tmp)):
two_sum[i] = l2_tmp[i] + l1_tmp[i] + flag
flag = 0
if two_sum[i] >= 10:
two_sum[i] = two_sum[i] - 10
flag = 1
if flag == 1:
two_sum.append(1)
return two_sum