minty_Brit666

Today’s blog is about the practice of the leetcode.
And I’ll give my own answer in this blog.

You are given two non-empty linked lists representing two non-negative integers. Each digit is stored in reverse order, and only one digit can be stored per node. You add the two numbers and return a linked list representing the sum in the same form. You can assume that neither of these numbers will start with 0, except for the number 0.

Source: LeetCode
Link: https://leetcode-cn.com/problems/add-two-numbers
Copyright belongs to the Collar buckle network. Commercial reprint please contact official authorization, non-commercial reprint please indicate the source.

class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
# Create a head node with the value of None, sum and a to point to, sum to return last, and a to traverse
sum = a = ListNode(None)
x = 0 # Initialize carry s to 0
while l1 or l2 or x:
# If l1 or L2 is present, take the value of L1 + the value of l2 + x(x is initially 0, if there is a carry 1 below, add it next time)
x += (l1.val if l1 else 0) + (l2.val if l2 else 0)
a.next = ListNode(x % 10)
# a.next points to the new list to create a new list
a = a.next
#a traverses backwards
x //= 10
# In the case of carry, take the modulus, eg. x = 12, 12 // 10 = 1
l1 = l1.next if l1 else None
#If l1 is present, it is traversed backwards, otherwise None
l2 = l2.next if l2 else None
# If L2 exists, it is traversed backwards, otherwise None
return sum.next
# Returns the next node of sum, since sum points to an empty head node, which is the next node in the new list

class Solution:def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:sum = a = ListNode(None)          x = 0while l1 or l2 or x:x += (l1.val if l1 else 0) + (l2.val if l2 else 0)  a.next = ListNode(x % 10)a = a.nextx //= 10l1 = l1.next if l1 else Nonel2 = l2.next if l2 else Nonereturn sum.next

The solution is derived from LeetCode user Mengzhihen.

minty_Brit666‘s python practice no.2相关推荐

  1. mysql_dc.ncf_GitHub - ytyagi1025/PythonPractice: Python practice works

    PythonPractice 使用说明 使用db.sql建立mysql数据库 在wanghong.py的BoseModel定义里设置mysql的连接参数 安装python库pymysql, reque ...

  2. python音频聚类_利用python的KMeans和PCA包实现聚类算法

    题目: 通过给出的驾驶员行为数据(trip.csv),对驾驶员不同时段的驾驶类型进行聚类,聚成普通驾驶类型,激进类型和超冷静型3类 . 利用Python的scikit-learn包中的Kmeans算法 ...

  3. Python模块包中__init__.py文件的作用

    2019独角兽企业重金招聘Python工程师标准>>> 在eclipse中用pydev开发Python脚本时,我遇到了一个这样的现象,当我新建一个pydev package时,总会自 ...

  4. 学习Python编程的19个资源

    用Python编写代码一点都不难,事实上它一直被赞誉为最容易学的编程语言.如果你准备学习web开发, Python是一个不错的开始,甚至想做游戏的话,用Python来开发游戏的资源也有很多.这是快速学 ...

  5. python资料免费-python 资料

    广告关闭 腾讯云双11爆品提前享,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高满返5000元! github,是源码学习.版本控制不可缺少的网站,找源码学习请第一时间到此网站,f ...

  6. 基于python opencv实现广角相机标定和图像畸变矫正

    目的: 实现相机标定,得到相机的内参以及畸变旋转参数等 尝试矫正由相机产生的图像畸变 代码: import cv2 as cv import numpy as np import glob impor ...

  7. python no module name_python导包显示No module named XXX问题

    最近用sublime text写python脚本,在导包是一直显示No module named XXX. 问题描述: 首先文件夹的目录结构如下: count.py文件,代码如下: 1 #coding ...

  8. Python 实现类似sed命令的字符串替换小程序

    背景 sed命令 sed 's/原字符串/新字符串' 单引号中间是s表示替换,原字符串就是要被替换掉的字符串,新字符串就是想要的字符串. 效果 在命令行输入python practice.py i 3 ...

  9. python模块 init py_Python模块包中__init__.py文件的作用

    在eclipse中用pydev开发Python脚本时,我遇到了一个这样的现象,当我新建一个pydev package时,总会自动地生成一个空的__init__.py文件,因为是python新手,所以很 ...

最新文章

  1. C++ Primer 5th笔记(chap 17 标准库特殊设施)随机数发生器种子( seed)
  2. java正则表达式空行_正则表达式删除空行
  3. 安卓开发笔记——关于图片的三级缓存策略(内存LruCache+磁盘DiskLruCache+网络Volley)...
  4. vc++ 关于 指针操作
  5. 利用 %20 替换 空格
  6. 一个双线程下同一时候操作指针变量导致野指针出现的问题总结
  7. 符合W3C标准的target=_blank形式
  8. 从知网或PDF复制英文单词间隔过大问题
  9. 事件驱动的过程链方法(EPCs)
  10. MySQL 去重SQL
  11. xp系统如何更改计算机用户名,windows xp电脑如何设置、修改开机密码
  12. Java爬取喜马拉雅非付费音频【优化】
  13. 2022 第二届中国移动“梧桐杯”大数据应用创新大赛-基于移动大数据的网约车司机识别 线上0.95+ 方案
  14. Composer中的ThingWorx模型定义—建模
  15. 从零开始学WEB前端——JavaScript数据类型
  16. 买卖股票的最佳时间含手续费的代码实现
  17. 为什么很多人认为测试就是“鼠标点点点”?
  18. Java xml文件解析
  19. hibernate数据检索策略
  20. JAVA与MAVEN打包

热门文章

  1. 发布【水晶五笔】安卓版
  2. 一、JDK、JRE、JVM的区别
  3. 玩转github客户端
  4. 基于Qt的组态监控软件实现以及分析(转)
  5. GEE随机(一):利用NDVI和NDWI阈值提取区域裸土用地类型
  6. 世界需要一种什么样的语言?
  7. 夯实Java基础系列6:一文搞懂抽象类和接口,从基础到面试题,揭秘其本质区别!
  8. QWebEngineView无法打开二级页面
  9. java多线程与并发
  10. 硬盘逻辑锁解锁简单方法