题目描述:
方法一:
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = Noneclass Solution:def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:cur_l1 = l1cur_l2 = l2cur = ListNode(0)head = curflag = 0while cur_l1 != None or cur_l2 != None:value = 0value += flagif cur_l1 != None:value += cur_l1.valif cur_l2 != None:value += cur_l2.valnew_node = ListNode(0)cur.next = new_nodecur = cur.nextcur.val = value % 10flag = value // 10if cur_l1 != None:cur_l1 = cur_l1.nextif cur_l2 != None:cur_l2 = cur_l2.nextif flag != 0:new_node = ListNode(0)cur.next = new_nodecur = cur.nextcur.val = flagreturn head.next