一开始没在意给的是自定义的数据结构链表,直接用list去做了,开始的代码如下:
def addTwoNumbers(l1, l2):# 计算l1和l2的长度len1 = len(l1)len2 = len(l2)# 计算l1的值l1_value = 0for i in range(len1):index = len1 - i - 1num_mul = indexnum_data = l1[index]while num_mul!=0:num_data = num_data * 10num_mul = num_mul - 1l1_value = l1_value + num_data# 计算l2的值l2_value = 0for i in range(len2):index = len2 - i - 1num_mul = indexnum_data = l2[index]while num_mul!=0:num_data = num_data * 10num_mul = num_mul - 1l2_value = l2_value + num_data# 得到反向的加和结果sum_value = l1_value + l2_valuey = list()while sum_value != 0:y.append(sum_value % 10)sum_value = int(sum_value/10)return ya = list()
a.append(2)
a.append(4)
a.append(3)
b = list()
b.append(5)
b.append(6)
b.append(4)
print(addTwoNumbers(a,b))
然后发现,要使用它给的自定义的list,然后修改了代码,将自定义的转化为python自带的,信心满满去提交,结果血崩:
class ListNode:def __init__(self, x):self.val = xself.next = Nonedef addTwoNumbers(l1, l2):# 将自定义的结构转化为python自带的listk1 = list()k2 = list()while l1!=None:k1.append(l1.val)l1 = l1.nextwhile l2!=None:k2.append(l2.val)l2 = l2.next# 计算l1和l2的长度len1 = len(k1)len2 = len(k2)# 计算l1的值l1_value = 0for i in range(len1):index = len1 - i - 1num_mul = indexnum_data = k1[index]while num_mul!=0:num_data = num_data * 10num_mul = num_mul - 1l1_value = l1_value + num_data# 计算l2的值l2_value = 0for i in range(len2):index = len2 - i - 1num_mul = indexnum_data = k2[index]while num_mul!=0:num_data = num_data * 10num_mul = num_mul - 1l2_value = l2_value + num_data# 得到反向的加和结果sum_value = l1_value + l2_valuey = ListNode(0)x = ywhile sum_value != 0:y.val = sum_value % 10sum_value = int(sum_value / 10)if sum_value!=0:y.next = ListNode(0)y = y.nextreturn x
额,给了很大的数,估计就崩在这里了;
简单想了想,查了一下,python3没有长整型,完犊子,不能用两个数相加来做了,必须重构代码了,血崩!!!
先去打个球吃个饭,哈哈哈
回来了,好吧,一个字符一个字符相加判断吧,哭哭...
做出来了,中间出了一堆bug
最终成功代码:
class ListNode:def __init__(self, x):self.val = xself.next = Nonedef addTwoNumbers(l1, l2):y = ListNode(0)x = yadd_one = 0# 当两个都不为空时while l1 != None and l2 != None:print('111')if l1.val + l2.val >= 10:y.next = ListNode(0)y = y.nexty.val = ((l1.val + l2.val) % 10 + add_one)add_one = 1l1 = l1.nextl2 = l2.nextelse:y.next = ListNode(0)y = y.nextif l1.val + l2.val + add_one >= 10:y.val = (l1.val + l2.val + add_one) % 10add_one = 1else:y.val = (l1.val + l2.val + add_one)add_one = 0l1 = l1.nextl2 = l2.nextif l1 == None and l2 != None:print('222')while l2 != None:y.next = ListNode(0)y = y.nextif l2.val + add_one >= 10:y.val = (l2.val + add_one) % 10add_one = 1else:y.val = (l2.val + add_one)add_one = 0l2 = l2.nextif l2 == None and l1 != None:print('333')while l1 != None:y.next = ListNode(0)y = y.nextif l1.val + add_one >= 10:y.val = (l1.val + add_one) % 10add_one = 1else:y.val = (l1.val + add_one)add_one = 0l1 = l1.nextif l1 == None and l2 == None:if add_one == 1:print('444')y.next = ListNode(1)y = y.nexty.val = 1return x.nextreturn x.nextl1 = ListNode(1)
l1.next = ListNode(0)
l1.next.next = ListNode(0)
l1.next.next.next = ListNode(0)
l1.next.next.next.next = ListNode(0)
l1.next.next.next.next.next = ListNode(0)
l1.next.next.next.next.next.next = ListNode(0)
l1.next.next.next.next.next.next.next = ListNode(0)
l1.next.next.next.next.next.next.next.next = ListNode(0)
l1.next.next.next.next.next.next.next.next.next = ListNode(0)
l1.next.next.next.next.next.next.next.next.next.next = ListNode(0)
l1.next.next.next.next.next.next.next.next.next.next.next = ListNode(0)
l1.next.next.next.next.next.next.next.next.next.next.next.next = ListNode(0)
l1.next.next.next.next.next.next.next.next.next.next.next.next.next = ListNode(0)
l1.next.next.next.next.next.next.next.next.next.next.next.next.next.next = ListNode(0)
p = l1.next.next.next.next.next.next.next.next.next.next.next.next.next.next
p.next = ListNode(0)
p.next.next = ListNode(0)
p.next.next.next = ListNode(0)
p.next.next.next.next = ListNode(0)
p.next.next.next.next.next = ListNode(0)
p.next.next.next.next.next.next = ListNode(0)
p.next.next.next.next.next.next.next = ListNode(1)l2 = ListNode(3)
l2.next = ListNode(7)
l3 = ListNode(9)
l3.next = ListNode(2)l4 = ListNode(5)
l5 = ListNode(5)l6 = ListNode(9)
l7 = ListNode(9)
l7.next = ListNode(9)y = addTwoNumbers(l2,l3)
while y.next!=None:print(y.val)y = y.next
这道题其实蛮简单的,没有什么复杂性,主要是细节要注意。
今日leetecode完成!