leetcode 链表 两数相加

时间:2023-03-09 23:51:41
leetcode 链表 两数相加
 两数相加

给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。

你可以假设除了数字 0 之外,这两个数字都不会以零开头。

示例:

输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
 # Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None class Solution:
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
head = ListNode(0)
cur = head
m = 0
while True:
if l1 is not None:
a = l1.val
else:
a = 0
if l2 is not None:
b = l2.val
else:
b = 0
if l1 is None and l2 is None and m == 0:
return head.next
else:
add = a + b + m
cur.next = ListNode(add % 10)
m = (a+b+m) // 10
cur = cur.next
if l1 is not None:
l1 = l1.next
if l2 is not None:
l2 = l2.next