leetcode -腾讯50题

版权声明:本文由lianyhai编写,不得用于商业用途,其他用途请随便。如果非要用做商业用途请给我微信打一下钱谢谢!哈哈哈哈 https://blog.csdn.net/qq_36303521/article/details/90085007

两数相加

题解

> # 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:
>         if not l1: return l2
>         if not l2: return l1
>         
>         phead = ListNode(0)
>         p=phead
>         cur=0
>         while l1 or l2:
>             x = l1.val if l1 else 0
>             y = l2.val if l2 else 0
>             s= cur+x+y
>             cur = s//10
>             p.next = ListNode(s%10)
>             p = p.next
>             if l1!= None : l1 = l1.next
>             if l2!= None : l2 = l2.next
>         if cur>0:
>             p.next = ListNode(1)
>         return phead.next

猜你喜欢

转载自blog.csdn.net/qq_36303521/article/details/90085007
今日推荐