给定两个非空链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储单个数字。将这两数相加会返回一个新的链表。
你可以假设除了数字 0 之外,这两个数字都不会以零开头。
进阶:
如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转。
示例:
输入: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
输出: 7 -> 8 -> 0 -> 7
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
def putIntoStack(list, list2, stack, stack2):
while list:
stack.append(list.val)
list = list.next
while list2:
stack2.append(list2.val)
list2 = list2.next
stack = []
stack2 = []
putIntoStack(l1, l2, stack, stack2)
jinwei = 0
head = ListNode(-1)
while stack or stack2 or jinwei:
num = 0
num2=0
if stack:
num = stack.pop()
if stack2:
num2 = stack2.pop()
jinwei, mod = divmod(num+num2+jinwei,10)
newnode = ListNode(mod)
newnode.next = head.next
head.next = newnode
return head.next
#(7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
head = ListNode(7)
a = ListNode(2)
b = ListNode(4)
c = ListNode(3)
head.next = a
a.next = b
b.next = c
head2 = ListNode(5)
d = ListNode(6)
e = ListNode(4)
head2.next = d
d.next = e
head3 = Solution().addTwoNumbers(head, head2)
while head3:
print(head3.val)
head3 = head3.next