【LeetCode】2.两数相加

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time: 2019/3/22
# @Author: xfLi
# The file...


class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

def addTwoNumbers(l1, l2):
    temp = ListNode(0)
    l3 = temp
    add_two = 0
    while l1 or l2  or add_two != 0:
        if l1:
            add_two += l1.val
            l1 = l1.next
        if l2:
            add_two += l2.val
            l2 = l2.next
        temp.next = ListNode(add_two % 10)
        temp = temp.next
        add_two = add_two // 10
    return l3.next

猜你喜欢

转载自blog.csdn.net/qq_30159015/article/details/88774576