# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com'
https://oj.leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two non-negative numbers.
The digits are stored in reverse order and each of their nodes contain a single digit.
Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8 ===Comments by Dabay===
这道题没有要求不使用额外的空间。思路比较简单,就是逐个加起来的同时考虑进位。
那就在已有的空间上操作,主要是处理一些细节问题,如末尾有进位、两个数组不一样长等等。
我这里为了省事,当l1不够l2长的时候,用了额外的空间。
''' # Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None class Solution:
# @return a ListNode
def addTwoNumbers(self, l1, l2):
if l1 is None:
return l2
if l2 is None:
return l1 node1, node2, carry, end = l1, l2, 0, None
while node1 or node2:
if not node1:
node1 = ListNode(0)
end.next = node1
if not node2:
node2 = ListNode(0)
v = node1.val + node2.val + carry
carry = 0
if v >= 10:
v = v - 10
carry = 1
node1.val = v
end = node1
node1 = node1.next
node2 = node2.next
if carry:
end.next = ListNode(1)
return l1 def main():
root1 = ListNode(2)
root1.next = ListNode(4)
root1.next.next = ListNode(3)
root2 = ListNode(5)
root2.next = ListNode(6)
root2.next.next = ListNode(4)
s = Solution()
root = s.addTwoNumbers(root1, root2)
node = root
while node:
print "%s->" % node.val,
node = node.next
print "None" if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[LeetCode][Python]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 ...

随机推荐

  1. php获取当前日期-7天

    // 将目前的时间戳值放入一数组内$strdate = '2014-02-03';$desDate = strtotime($strdate);//var_dump($desDate); $times ...

  2. 测试Web服务接口

    1. http://www.iteye.com/topic/142034 2. http://www.iteye.com/topic/1123835 3.http://yongguang423.ite ...

  3. SQL PLUS远程连接

    http://blog.csdn.net/wildin/article/details/5850252 这篇文章无敌了. Oracle sqlplus添加历史记录功能: http://www.cnbl ...

  4. 通过jpegoptim批量压缩文件

    #!/bin/sh filelist=$(ls) for file in $filelist do if [ -d $file ] then du -h $file /usr/local/bin/jp ...

  5. hdu 5256 序列变换 (LIS变形)

    序列变换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  6. jQuery+PHP实现的砸金蛋中奖程序

    准备 我们需要准备道具(素材),即相关图片,包括金蛋图片.蛋砸碎后的图片.砸碎后的碎花图片.以及锤子图片. HTML 我们页面上要展现的是一个砸金蛋的台子,台上放了编号为1,2,3的三个金蛋,以及一把 ...

  7. ActionBar隐藏修改图标和标题

    有时候在一些子页面或者内容页面,不需要显示ActionBar的标题栏图标.可用如下方式进行设置. 首先获取到ActionBar对象 ActionBar actionBar=getActionBar() ...

  8. Sizzle之tokenize

    在Sizzle里,大体思路,当为复合选择器时,判断是否支持querySeletorAll,如果不支持则调用自写方法select. select的功能十分冗长,下面先分析tokenize 在tokeni ...

  9. js split函数用法总结

    一.split定义:split() 方法用于把一个字符串分割成字符串数组, 返回值: 一个字符串数组. 二.基本用法:stringObject.split(separator,howmany) 1.参 ...

  10. 无法识别的属性“targetFramework”

    问题描述:无法识别的属性“targetFramework”.请注意属性名称区分大小写. 解决办法:修改.NET Framework 版本为相应版本即可,例如2.0换成4.0. 参考:http://bl ...