2. Add Two Numbers

You are given two non-empty linked lists representing two non-negative integers. 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.

You may assume the two numbers do not contain any leading zero, except the number 0 itself

Example

Input: ( ->  -> ) + ( ->  -> )
Output: -> ->
Explanation: + = .
 # Definition for singly-linked list.
#class ListNode:
# 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
"""
if l1==None:
return l2
if l2==None:
return l1
carry=
s=ListNode()
ret=s
while l1 or l2:#如果两个链表next均不为空
sum=
if l1:
sum+=l1.val
l1=l1.next
if l2:
sum+=l2.val
l2=l2.next
sum+=carry
s.next=ListNode(sum%)
s=s.next
carry=(sum>=)
if carry==:
s.next=ListNode()
del s
return ret.next

ANSWER

(python)leetcode刷题笔记 02 Add Two Numbers的更多相关文章

  1. 【leetcode刷题笔记】Add Two Numbers

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

  2. (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters

    3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...

  3. (python)leetcode刷题笔记05 Longest Palindromic Substring

    5. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...

  4. (python)leetcode刷题笔记04 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectiv ...

  5. 【leetcode刷题笔记】Add Binary

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  6. leetcode刷题: 002 Add Two Numbers

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

  7. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  8. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  9. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

随机推荐

  1. CMarkup成员方法简介 (转)

    CMarkup成员方法简介 (转) 转自:http://blog.csdn.net/magictong/article/details/6669837翻译:magictong(童磊)2011年7月版权 ...

  2. TCP Flow Control and Data Transfer

    TCP Flow Control TCP Data Transfer Selective Repeat ARQ with Positive ACK Window slides a byte basis ...

  3. Vue多个元素展开收起

    html: <div class="helpPages_main"> <div class="read" v-for="(item, ...

  4. 协议类接口 - LCD

    一.引脚含义 下图为某LCD相关引脚: 从引脚可以大概看出其SoC的连接情况: 1)VCLK为时钟,每一次像素就移动一次 2)HSYNC/VLINE 3)VSYNC/VFRAME 4)VD0 - VD ...

  5. webpack之理解loader

    我们在写webpack配置文件的时候,应该有注意到经常用到loader这个配置项,那么loader是用来做什么的呢? loader其实是用来将源文件经过转化处理之后再输出新文件. 如果是数组形式的话, ...

  6. 在SQL Server中批量修改有规律列的定义

    )=N'要修改的表名'; --修改所有以sl结尾的列名的小数位数为4位 select syscolumns.name into #t1 from syscolumns,systypes where s ...

  7. Java实现“睡排序”——线程池Executors的使用

    前提 之前在知乎上看见一个有意思的排序算法——睡排序. 睡排序最早好像是4chan上一个用户用shell脚本实现的: 算法思想简洁明了:利用进程的sleep来实现 越大的数字越迟输出. 虽然像2L说的 ...

  8. jQuery实现简单的拼图游戏

    一,实现拼图的搭建: <div class="box"> <table id="table1" class="mytable&quo ...

  9. Jquery中数值求和及根据余数改变对应的数值样式

    Jquery内容 <script type="text/javascript"> $(function () { var num = 0; $(".box o ...

  10. 【JavaWeb】从零实现用户登录

    1.数据库预备 1.1 SQL 创建数据库 create database db; 创建表 create table userInfo( id int primary key , name ), pa ...