Q: 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

 
keys: 1. 使用dummy node记录head. 
2. 对于两个list的操作,一般使用 while l1 or l2, if l1, if l2 来处理两个list不一样长的情况。
 class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
dummy = cur = ListNode(0)
carry = 0 while l1 or l2:
a = l1.val if l1 else 0
b = l2.val if l2 else 0 sum = a+b+carry
cur.next = ListNode(sum%10)
carry = sum/10
cur = cur.next if l1:
l1 = l1.next
if l2:
l2 = l2.next if carry > 0:
cur.next = ListNode(1) return dummy.next
 

Leetcode #2 Add two number的更多相关文章

  1. LeetCode 面试:Add Binary

    1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...

  2. LeetCode:1. Add Two Numbers

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

  3. C#版 - Leetcode 414. Third Maximum Number题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

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

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

  5. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

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

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

  7. 【一天一道LeetCode】#260. Single Number III

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  8. [LeetCode] 415. Add Strings_Easy tag: String

    Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...

  9. 乘风破浪:LeetCode真题_009_Palindrome Number

    乘风破浪:LeetCode真题_009_Palindrome Number 一.前言 如何判断一个整型数字是回文呢,我们可能会转换成String来做,但是还有更简单的方法. 二.Palindrome ...

随机推荐

  1. 转载和积累系列 - 深入理解HTTP协议

    深入理解HTTP协议 1. 基础概念篇 1.1 介绍 HTTP是Hyper Text Transfer Protocol(超文本传输协议)的缩写.它的发展是万维网协会(World Wide Web C ...

  2. python中的字符串操作

    #!/usr/bin/python # -*- coding: UTF-8 -*- ''' str.capitalize() ''' str = 'this is a string example' ...

  3. jQuery获取文本节点之 text()/val()/html() 方法区别

    1. 无参html():取得第一个匹配元素的html内容.这个函数不能用于XML文档.但可以用于XHTML文档,返回的是一个String 例子: html页面代码:<div><p&g ...

  4. HDU 1022 Train Problem I

    A - Train Problem I Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  5. Java集合系列:-----------04fail-fast总结(通过ArrayList来说明fail-fast的原理以及解决办法)

    前面,我们已经学习了ArrayList.接下来,我们以ArrayList为例,对Iterator的fail-fast机制进行了解.内容包括::1 fail-fast简介2 fail-fast示例3 f ...

  6. intellij idea 高级用法之:集成JIRA、UML类图插件、集成SSH、集成FTP、Database管理

    之前写过一篇IntelliJ IDEA 13试用手记,idea还有很多高大上的功能,易用性几乎能与vs.net媲美,反正我自从改用idea后,再也没开过eclipse,今天来看几个高级功能: 一.与J ...

  7. ROS系统python代码测试之rostest

    ROS系统中提供了测试框架,可以实现python/c++代码的单元测试,python和C++通过不同的方式实现, 之后的两篇文档分别详细介绍各自的实现步骤,以及测试结果和覆盖率的获取. ROS系统中p ...

  8. SM2国密证书合法性验证

    通常我们遇到过的X509证书都是基于RSA-SHA1算法的,目前国家在大力推行国密算法,未来银行发行的IC卡也都是基于PBOC3.0支持国密算法的,因此我们来学习一下如何验证SM2国密证书的合法性.至 ...

  9. web安全——应用(java)

    简介 由于网络技术日趋成熟,黑客们也将注意力从以往对网络服务器的攻击逐步转移到了对web应用的攻击.据最新调查,信息安全有75%都发生在web应用而非网络层面. 场景 控制访问的权限.只让可以访问的访 ...

  10. js队列

    用指针和数组模拟基本队列 http://blog.csdn.net/zhuwq585/article/details/53177192 js下的事件队列,或者异步队列 http://www.jb51. ...