[LeetCode][Python]Add Two Numbers
# -*- 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的更多相关文章
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- LeetCode:1. Add Two Numbers
题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...
- LeetCode 面试:Add Two Numbers
1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- LeetCode #002# Add Two Numbers(js描述)
索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...
- LeetCode 2. add two numbers && 单链表
add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...
- [Leetcode Week15] Add Two Numbers
Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
- [LeetCode] 2. Add Two Numbers 两个数字相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
随机推荐
- jupyter巨好玩-简介与安装
ipython notebook改名jupyter了而且更好玩更好用 jupyter简介 jupyter是啥啊? 这个要从ipython说起,ipython是个交互式的python的解释器,自带颜色, ...
- ffmpeg在Win7 VS2010中debug通过,release出错的问题解决方法
我所用的系统环境是Win7 32位操作系统+VS2010编译环境.所以在debug模式下调通之后,在Release模式下调试不通过,最后通过上网查资料和自己对比两个编译选项得出以下结论: 修改“项目- ...
- java中等于和equals的区别
面试的时候没答上,很受打击,特写在这里. ==是判断两个变量或实例是不是指向同一个内存空间equals是判断两个变量或实例所指向的内存空间的值是不是相同 除了String和封装器,equals()和“ ...
- javascript函数值的重写
原文:javascript函数值的重写 javascript函数值的重写 定义了一个函数,需要重写这个函数并使用原先的函数值.做法是: 1.定义一个变量让原先函数的值指向它,把原先函数的指向一个新的函 ...
- 安装Logstash
安装Logstash: Logstash 需要 Java 7或者以后版本,使用官方的Oracle发布或者一个开源发布版本比如OpenJDK 检查Java 版本,运行下面的命令: zjtest7-fro ...
- qt运行库
KERNEL32.DLL MINGWM10.DLL MSVCRT.DLL LIBGCC_S_DW2-1.DLL QTCORE4.DLL QTGUI4.DLL 笔者安装的是QT SDK.(发行版本这是前 ...
- getopt vs getopts
getopt示例 #!/bin/bash aflag=no args=`getopt a: $@` ]; then echo 'Usage: ...' exit fi set -- $args ] d ...
- Spring、Struts2+Spring+Hibernate整合步骤
所使用的Jar包: Hibernate: Spring(使用MyEclipse自动导入框架功能) Struts2: 注解包和MySql驱动包: 1.配置Hibernate和Spring: <be ...
- Necklace of Beads(polya计数)
Necklace of Beads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7451 Accepted: 3102 ...
- Dark roads(kruskal)
Dark roads Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Su ...