题目来源:

https://leetcode.com/problems/add-two-numbers/


题意分析:

这道题目是要将两个单链条相加。输出得到的新链条。


题目思路:

不难发现,其实题目就是要我们模拟加法的实现。那么,我们就直接从低位(链条第一位)开始,同位相加,满10就往高位+1。


代码(python):

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
ans = ListNode(0)
tmp = ans
tmpsum = 0
while True:
if l1 != None:
tmpsum += l1.val
l1 = l1.next
if l2 != None:
tmpsum += l2.val
l2 = l2.next
tmp.val = tmpsum % 10
tmpsum //= 10
if l1 == None and l2 == None and tmpsum == 0:
break
tmp.next = ListNode(0)
tmp = tmp.next
return ans

PS:要注意的是,不要忘记最后的那个+1,比如 1-> 5 + 2->5  = 3-> 0 –> 1


转载请注明出处:http://www.cnblogs.com/chruny/

[LeetCode]题解(python):002-Add Two Numbers的更多相关文章

  1. Leetcode 第 2 题(Add Two Numbers)

    Leetcode 第 2 题(Add Two Numbers) 题目例如以下: Question You are given two linked lists representing two non ...

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

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

  3. No.002 Add Two Numbers

    Add Two Numbers Total Accepted: 160702 Total Submissions: 664770 Difficulty: Medium You are given tw ...

  4. LeetCode--No.002 Add Two Numbers

    Add Two Numbers Total Accepted: 160702 Total Submissions: 664770 Difficulty: Medium You are given tw ...

  5. LeetCode解题笔记 - 2. Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

  6. leetcode刷题: 002 Add Two Numbers

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

  7. 【LeetCode】002 Add Two Numbers

    题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...

  8. 【JAVA、C++】LeetCode 002 Add Two Numbers

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

  9. [Leetcode] 002. Add Two Numbers

    https://leetcode.com/problems/add-two-numbers/ public class Solution { public ListNode addTwoNumbers ...

  10. 002 Add Two Numbers 链表上的两数相加

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

随机推荐

  1. Socks5协议中文文档

    译者:Radeon(Radeon bise@cmmail.com) 译文公布时间:2001-6-18 文件夹 1.介绍 2.现有的协议 3.基于TCP协议的客户 4.请求 5.地址 6.应答 7.基于 ...

  2. Android SQLite 事务处理

    应用程序初始化时需要批量的向sqlite中插入大量数据,单独的使用for+Insert方法导致应用响应缓慢,因为 sqlite插入数据的时候默认一条语句就是一个事务,有多少条数据就有多少次磁盘操作.我 ...

  3. md笔记——正则学习

    正则表达式 在线调试正则1 在线调试正则2 规则记录 \d 匹配一个数字字符.等价于[0-9] \D 匹配一个非数字字符.等价于[^0-9]. . 通配符,可以匹配任意字符. ? 表示量词" ...

  4. Deep Learning(深度学习)学习笔记整理系列之(二)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  5. java 设计模式初探之适配器模式

    1. 概述 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 2. 解决的问题 即Adapter模式使得原本由于接口不兼容而不 ...

  6. html5 meta标签

     <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-sca ...

  7. 代码风格——Cocos2d-x学习历程(四)

    1.Cocos2d-x拥有一个包含其他所有头文件的文件"cocos2d.h".通常,我们只需要在使用时包含这个头文件,就可以使用引擎的全部功能了. 2.Cocos2d-x的类都放置 ...

  8. IT工程师值得一看的书籍

    http://blog.csdn.net/chinahuyong/article/details/45060203

  9. php以fastCGI的方式运行在iis下,遇到的文件系统权限问题及解决方法

    今天准备将一个php demo放在IIS下运行,网站在IIS下的配置是这样的: 应用程序池是集成模式下的.net framework 2.0(2.0或4.0没什么关系,因为php以fastCGI的方式 ...

  10. python 时间字符串与日期转化

    python 时间字符串与日期转化 datetime.datetime.strptime(string, format) 根据指定的格式解析字符串为一个datetime类型.相当于datetime.d ...