• Leetcode2-AddTwoNumbers


    1. 题目

    You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

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

    在这里插入图片描述
    在这里插入图片描述

    2. 分析

    2.1 分解问题

    • 需要将列表转换成单向链表形式
    • 需要逐个相加
    • 需要将链表打印出来

    2.2 代码-python

    from typing import List
    from typing import Optional
    
    # 定义节点,val表示节点的值
    # next 表示下一个节点
    class ListNode:
        def __init__(self, val=0, next=None):
            self.val = val
            self.next = next
    
    # 定义方案
    # 思路:
    # 1. 定义一个记录的节点dummy
    # 2. 定义一个循环的节点curr
    # 3. 定义一个求和的carry
    # 4. 逐位求和
    # 5. 迭代循环
    
    class Solution:
        def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> ListNode:
    
            dummy = ListNode(0)
            curr = dummy
            carry = 0
    
            while l1 or l2 or carry:
                if l1:
                    carry += l1.val
                    l1 = l1.next
    
                if l2:
                    carry += l2.val
                    l2 = l2.next
    
                curr.next = ListNode(carry % 10)
                carry //= 10
                curr = curr.next
    
            return dummy.next
    
        def createList(self, mylist: List) -> ListNode:
            output = ListNode(0)
            tmp = output
            for num in mylist:
                tmp.next = ListNode(num)
                tmp = tmp.next
    
            return output.next
    
    
    def display(mylistnode: ListNode):
        print("[", end=" ")
        temp = mylistnode
        while temp.next:
            print(temp.val, end=" , ")
            temp = temp.next
        print(temp.val, end=" ")
    
        print("]")
    
    
    if __name__ == "__main__":
        #    aa = [2, 4, 3]
        #    bb = [5, 6, 4]
        aa = [9, 9, 9, 9, 9, 9, 9]
        bb = [9, 9, 9, 9]
        myfun = Solution()
        list1 = myfun.createList(aa)
    
        list2 = myfun.createList(bb)
    
        result = myfun.addTwoNumbers(list1, list2)
    
        print(f"aa={aa}")
    
        print(f"bb={bb}")
    
        print(f"result=", end="")
        display(result)
    
    aa=[9, 9, 9, 9, 9, 9, 9]
    bb=[9, 9, 9, 9]
    result=[ 8 , 9 , 9 , 9 , 0 , 0 , 0 , 1 ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83

    2.3 LeetCode 结果

    在这里插入图片描述

  • 相关阅读:
    js和css会不会阻塞页面渲染?
    基于MWORKS.Sysplorer的油气混合作动筒建模与仿真应用
    暑期留校——区间DP-板子题石子合并
    C语言实验七 二维数组程序设计
    PG数据库内核源码分析——执行计划EXPLAIN
    【STL】set/multiset容器
    Flask 学习-18.配置管理开发/生产/测试环境
    挑选适合您的优秀项目管理软件
    STM32进阶笔记——复位、时钟与滴答定时器
    论文阅读_变分自编码器_VAE
  • 原文地址:https://blog.csdn.net/scar2016/article/details/126441880