Leetcode 之 Add Two Numbers
作者:
nkuhero | 来源:发表于
2017-11-07 17:47 被阅读0次# 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)
ptr = head
carry = 0
while True:
if l1 != None:
carry += l1.val
l1 = l1.next
if l2 != None:
carry += l2.val
l2 = l2.next
ptr.val = carry % 10
carry = int(carry / 10)
if l1 != None or l2 != None or carry != 0:
ptr.next = ListNode(0)
ptr = ptr.next
else:
break
return head
本文标题:Leetcode 之 Add Two Numbers
本文链接:https://www.haomeiwen.com/subject/vbwjmxtx.html
网友评论