题目来源:

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. hibernate中文查询时无查询结果

    原因很简单,问题在于我连接mysql用的url时,没有指定字符集,导致查询不到任何数据 问题出在 hibernate.xml配置文件中: 将 <property name="jdbcU ...

  2. at org.apache.jsp.index_jsp._jspInit(index_jsp.java:23)异常解决

    部署项目,启动tomcat一切正常.输入项目地址后 tomcat报例如以下错误: java.lang.NullPointerException at org.apache.jsp.index_jsp. ...

  3. 关于android:screenOrientation="portrait" 横竖屏切换

    当在AndroidManifest.xml文件中定义了android:screenOrientation="portrait",就表示当我们切换横竖屏的时候,屏幕的内容始终以竖屏显 ...

  4. bit-map牛刀小试:数组test[X]的值所有在区间[1, 8000]中, 现要输出test中反复的数。要求:1. 不能改变原数组; 2.时间复杂度为O(X);3.除test外空间不超过1KB

    先来看看这个题目:数组test[X]的值所有在区间[1, 8000]中. 现要输出test中反复的数.要求:1. 不能改变原数组; 2.时间复杂度为O(X);3.除test外空间不超过1KB. 好, ...

  5. web开发 - 从零开始 - 02 - 常用标签

    1.a  : href 属性为id,即为锚点:文件路径,则为下载链接: 新窗口打开文件,target = "_blank";当前窗口打开,target="_self&qu ...

  6. IIS7中配置脚本错误解决方案

    同一个项目, 又建另一站点(相同的物理路径,) ,结果出下上图404.0错误, 原来是win7下应用程序池默认的32应用程序属性影响,参考下图,设置为True.        同一个项目, 又建另一站 ...

  7. C++输出中文字符(转)

    C++输出中文字符 1. cout 场景1: 在源文件中定义 const char* str = "中文" 在 VC++ 编译器上,由于Windows环境用 GBK编码,所以字符串 ...

  8. Java threadpool机制深入分析

    简介 在前面的一篇文章里我对java threadpool的几种基本应用方法做了个总结.Java的线程池针对不同应用的场景,主要有固定长度类型.可变长度类型以及定时执行等几种.针对这几种类型的创建,j ...

  9. JAVA 语 言 如 何 进 行 异 常 处 理 , 关 键 字 : throws,throw,try,catch,final

    throws是获取异常throw是抛出异常try是将会发生异常的语句括起来,从而进行异常的处理,catch是如果有异常就会执行他里面的语句,而finally不论是否有异常都会进行执行的语句.

  10. day5_python学习笔记_chapter6_字符串列表元组

    1. 序列:seq[n], seq[x:y], seq * n序列重复n次,切片, 序列翻转 s=”abcde", s[::-1]="edcba" 内建函数:1. 类型转 ...