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. oracle的dual表
  2. 让人心动的电子工艺品
  3. linux查询超线程,【转】Linux下判断一台机器是多少路,多少核,是否超线程的方法...
  4. “纯金”卫星,撞向我们的“蛋壳时代”
  5. oracle t7-2报价,Sun/Oracle T7-2服务器主板7315607全新7318270原装7318240
  6. mysql高级查询 二_MySQL高级查询(二)
  7. 深入react技术栈(6):React和DOM
  8. 面试常备题---JVM加载class文件的原理机制
  9. 思科修复NSA报告的Nexus 交换机DoS漏洞及其它
  10. 基于深度学习的视觉目标跟踪方法介绍
  11. Linux文件系统管理
  12. Java飞机大战 项目-源码
  13. Excel 单元格自定义下拉菜单
  14. 数字计算机模拟人脑,人造突触问世计算机模拟人脑不是梦
  15. 计算机硬盘满了怎么解决,我的128G笔记本电脑硬盘又满了,怎么办?
  16. JS里的for…in和for…of的用法
  17. linux串口文件传输工具
  18. 【vscode软件安装配置vue】
  19. 【游戏开发小技】Unity通过UI全屏图来模糊场景画面(Shader | 模糊 | 滤镜 | Blur)
  20. 多线程实现4个窗口卖100张票

热门文章

  1. java向硬件发送信息_如何用电脑控制手机发短信 用JAVA 通过USB口
  2. 镇江SEO咨询:如何让百度收录快?
  3. 激光甲烷传感器的优势对比
  4. Unity学习之游戏初步策划
  5. 智能养老看护系统之养老院人员定位解决方案--新导智能
  6. 洛谷P1077 [NOIP2012 普及组] 摆花 (DP)
  7. 钢琴的音色特点用matlab描述6,如何使用matlab模拟钢琴音色?
  8. python制作七夕礼物_opencv+python制作硬核七夕礼物
  9. numpy实现K-means聚类算法(可选是否已知类别数)和VQ-LBG矢量量化算法以及散点数据生成(含完整实验报告)
  10. 嗜血法医第八季/全集Dexter 8迅雷下载