[日常 LeetCode] 2. Add Two Numbers

  |   0 评论   |   16 浏览

Q:
You are given twonon-emptylinked lists representing two non-negative integers. The digits are stored inreverse orderand each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example:

**Input:** (2 -> 4 -> 3) + (5 -> 6 -> 4)
**Output:** 7 -> 0 -> 8
**Explanation:** 342 + 465 = 807.

以下解答方式纯属抄的,尴尬的是好几天 没看懂题目
A:

# 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:
        c1=l1
        c2=l2
        c_val=''
        c2_val=''
        while c1:
            c_val += str(c1.val)
            c1=c1.next
        while c2:
            c2_val += str(c2.val)
            c2=c2.next
        c_val = c_val[::-1]
        c2_val=c2_val[::-1]

        added = int(c_val) + int(c2_val)

        added =str(added)
        added =added[::-1]

        LL=[]
        for i in added:

            LL.append(int(i))


        return LL
---------------------------------------------------------------
>> 博客地址:https://blog.mufengs.com
>> 邮箱地址:mufeng5619@gmail.com
>> 微信帐号:Do8080
>> Github : https://github.com/mufengcoding
---------------------------------------------------------------

评论

发表评论