• leetcode445 Add Two Numbers II


     1 """
     2 You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
     3 You may assume the two numbers do not contain any leading zero, except the number 0 itself.
     4 Follow up:
     5 What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
     6 Example:
     7 Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
     8 Output: 7 -> 8 -> 0 -> 7
     9 """
    10 """
    11 与leetcode2 类似。
    12 这两题都是自己做出来的
    13 """
    14 class ListNode:
    15     def __init__(self, x):
    16         self.val = x
    17         self.next = None
    18 
    19 class Solution:
    20     def addTwoNumbers(self, l1, l2):
    21         num1, num2 = 0, 0
    22         if l1 and not l2:
    23             return l1
    24         if l2 and not l1:
    25             return l1
    26         p1, p2 = l1, l2 #将链表l1 和 l2 转成数字num1 和num2 求和得num
    27         while p1:
    28             num1 = num1 * 10 + p1.val
    29             p1 = p1.next
    30         while p2:
    31             num2 = num2 * 10 + p2.val
    32             p2 = p2.next
    33         num = num1 + num2
    34         res = ListNode(0)  #头结点
    35         end = ListNode(num % 10) #尾结点
    36         res.next = end
    37         end.next = None
    38         num = num // 10
    39         while num:  #从链表尾部插入当前结点cur
    40             cur = ListNode(num % 10) #当前结点
    41             res.next = cur
    42             cur.next = end
    43             end = cur
    44             num = num // 10
    45         return res.next
  • 相关阅读:
    au 批处理 声音 插入空白
    加载字体
    AS2 继承
    an 跳转各个fla发布的html,并控制声音播放与停止
    两界面之间跳转
    AS3 实现过滤数组/删除数组中的相同元素(记录6种方法)
    as3 updateAfterEvent的作用
    egret 白鹭引擎遇到的问题和解决方案
    mysql内连接、左连接、右连接举例说明
    mysql常用函数示例
  • 原文地址:https://www.cnblogs.com/yawenw/p/12324186.html
Copyright © 2020-2023  润新知