給定兩個非空鏈表來代表兩個非負整數。數字最高位位於鏈表開始位置。它們的每個節點只存儲單個數字。將這兩數相加會返回一個新的鏈表。
你可以假設除了數字 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