• 题目描述:输入两个非空单链表,链表的每个结点的值是一个1位数整数,两个链表都是一个大整数每一位的逆序排序,求这两个链表代表的整数的和的链表值;

  • 思路:

  1. 分别遍历两个链表,转化成相应的整数,求和后把结果每一位转化成单链表即可;
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
a = b = 0
carry = 0
while l1:
a += l1.val * 10 ** carry
carry += 1
l1 = l1.next
carry = 0
while l2:
b += l2.val * 10 ** carry
carry += 1
l2 = l2.next
ret = a + b
h = m = ListNode(0)
if not ret:
return h
while ret:
m.next = ListNode(ret % 10)
ret /= 10
m = m.next
return h.next

Python 解leetcode:2. Add Two Numbers的更多相关文章

  1. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  2. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  3. LeetCode:1. Add Two Numbers

    题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...

  4. LeetCode 面试:Add Two Numbers

    1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  5. LeetCode #002# Add Two Numbers(js描述)

    索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...

  6. LeetCode 2. add two numbers && 单链表

    add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...

  7. [Leetcode Week15] Add Two Numbers

    Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...

  8. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

  9. [LeetCode] 2. Add Two Numbers 两个数字相加

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  10. LeetCode之Add Two Numbers

    Add Two Numbers 方法一: 考虑到有进位的问题,首先想到的思路是: 先分位求总和得到 totalsum,然后再将totalsum按位拆分转成链表: ListNode* addTwoNum ...

随机推荐

  1. [Luogu] 八数码难题

    https://www.luogu.org/problemnew/show/P1379 long long ago 暴力bfs #include <iostream> #include & ...

  2. Density of Power Network(ZOJ 3708)

    Problem The vast power system is the most complicated man-made system and the greatest engineering i ...

  3. Spring事务管理的一些注意点

    在<Spring Boot事务管理(下)>中,已经介绍了如果在 protected.private 或者默认可见性的方法上使用@Transactional,事务将是摆设,也不会抛出任何异常 ...

  4. 未能加载文件或程序集“Microsoft.SqlServer.Management.Sdk.Sfc, Version=11.0.0.0, Culture=neutral, PublicKeyToken...

    刚开始看老师 用VS新建一个“ADO.NET 实体数据模型” 但是一直报错:未能加载文件或程序集“Microsoft.SqlServer.Management.Sdk.Sfc, Version=11. ...

  5. onselectstart与onselect—禁止选择或禁止复制

    这两个事件看起来很相似,事实上却非常的不同. onselectstart 使用js禁止用户选中网页上的内容,IE及Chrome下的方法一样.使用onselectstart,例如 IE: <bod ...

  6. Axure实现提示文本单击显示后自动消失的效果

    Axure实现提示文本单击显示后自动消失的效果 方法/步骤     如图所示,框出的部分为提示文本(已经命名为tooltip),希望达到的效果是默认加载时不显示,点击帮助图标后显示,且2秒后自动消失. ...

  7. flask_security学习笔记

    [Flask Security]当不能通过认证的时候制定跳转   Flask Security这个插件能对用户权限进行很好的控制.通过三个model实现:User,存放用户数据Role,存放角色数据U ...

  8. 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_11-修改页面-前端-Api调用

    修改数据 这是提交按钮的事件editSubmit 注意修改的方法是put方法.所以用requestPut 操作成功自动返回 测试 成功后自动跳转到列表页 修改后的数据

  9. 增加github访问速度

    转自:https://blog.csdn.net/qq_38977097/article/details/80770987 原因 为什么慢?github的CDN被某墙屏了. 解决方法 绕过dns解析, ...

  10. python抽取指定url页面的title方法

    python抽取指定url页面的title方法 今天简单使用了一下python的re模块和lxml模块,分别利用的它们提供的正则表达式和xpath来解析页面源码从中提取所需的title,xpath在完 ...