```# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param head1 ListNode类
# @param head2 ListNode类
# @return ListNode类
#
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
#
# @param head1 ListNode类
# @param head2 ListNode类
# @return ListNode类
#
class Solution:
def addInList(self , head1 , head2 ):
h1=self.reverse(head1)
h2=self.reverse(head2)
N=ListNode(-1)
N1=N
c=0
while h1 is not None and h2 is not None:
m=(h1.val+h2.val+c)%10
c=(h1.val+h2.val+c)//10
p=ListNode(m)
N1.next=p
N1=N1.next
h1=h1.next
h2=h2.next
while h1 is not None:
m=(h1.val+c)%10
c=(h1.val+c)//10
p=ListNode(m)
N1.next=p
N1=N1.next
h1=h1.next
while h2 is not None:
m=(h2.val+c)%10
c=(h2.val+c)//10
p=ListNode(m)
N1.next=p
N1=N1.next
h2=h2.next
if c==1:
N1.next=ListNode(1)
N1=N1.next
return self.reverse(N.next)
def reverse(self,head):
last=None
while head is not None:
temp=last
last=head
head=head.next
last.next=temp
return last
##### 反转链表,这么做虽然代码啰嗦了点,但是不回超时。