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.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
问题很明确,就是简单的加法,中间也只需要考虑进位就行了。
下面的逻辑,自认为是完全没有问题的。之所以出错是一个很坑爹的原因。节省时间可以合并a=1, b=2
为a,b = 1, 2
没有耦合的时候的确可以,我这里分开之后就不出错了,很神奇。。。
# 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
"""
res = ListNode(-1)
temp2 = 0
while l1!= None:
temp = l1.val + l2.val + temp2
if temp >=10:
temp2, res.val, res.next = 1, temp/10, res
else:
temp2, res.val, res.next = 0, temp, res
l2, l1 = l2.next, l1.next
print(res.val)
return res.next
上面为什么错了呢?是因为一个坑爹的问题,这里记录一下,就是temp2, res.val, res.next = 1, temp/10, res
或者temp2, res.next, res=1, ListNode(temp%10), res.next
这样的表达,难道只有在相互之间没有耦合的情况下才可以使用? 但是Palindrome Linked List 的解法当中出现了 rev, rev.next, slow = slow, rev, slow.next #slow要比rev快一步。
这种操作啊。。。背后的原因还没有探究清楚。
接下来是自己调节运行通过的代码
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
res_return = ListNode(-1)
res= res_return
temp2 = 0
while l1:
temp = int(l1.val) + int(l2.val if l2 else 0) + temp2
if temp >=10:
#temp2, res.next, res=1, ListNode(temp%10), res.next
temp2 = 1
res.next = ListNode(temp%10)
res = res.next
else:
#temp2, res.next, res = 0, ListNode(temp), res.next
temp2 = 0
res.next = ListNode(temp)
res = res.next
l1 = l1.next
if l2:
l2 = l2.next
else:
pass
while l2:#后面这些事 报错之后,处理l1和l2长度不一样的时候的情况。
if temp2 == 1:
if (l2.val+1)>=10:
temp2 = 1
res.next = ListNode((l2.val+1)%10)
res = res.next
else:
temp2 = 0
res.next = ListNode(l2.val+1)
res = res.next
else:
res.next = ListNode(l2.val)
res = res.next
l2 = l2.next
if temp2 == 1:#处理最后需要进位的情况,比如9+1=10
res.next = ListNode(1)
return res_return.next
我本地调试的时候, l2.val+1
这个操作还需要类型转换 int(l2.val)+1
,很奇怪。python版本应该是一样的Python2.7才对。
接下来,是网上的例子,尝试的确通过了
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
l1,l2为输入的待加和的两个串
"""
#将两个单链表串按位扫到列表listnum1和listnum2中
listnum1 = []
listnum2 = []
while l1 != None:
listnum1.append(l1.val)
l1 = l1.next
while l2 != None:
listnum2.append(l2.val)
l2 = l2.next
#将两个数用整型num1和num2表示出来(**运算为指数运算,eg. 2 ** 3 结果为8)
num1 = 0
num2 = 0
for i in range(len(listnum1)):
num1 = listnum1[i] * (10 ** i) + num1
for j in range(len(listnum2)):
num2 = listnum2[j] * (10 ** j) + num2
#计算结果后,构造结果的单链表结构l3
result = num1 + num2
l3 = ListNode(0)
p = ListNode(0)
p = l3
#l3和p指向首节点,构造过程中l3不动,仍指向首节点,p进行构造移动
while result >= 10:
temp = ListNode(None)
p.val = result % 10
p.next = temp
p = temp
result = result / 10
#由于循环到最后一个节点时不再构造新节点,于是退出循环,并给最后一个节点赋值
p.val = result % 10
return l3