(python)leetcode刷题笔记 02 Add Two Numbers
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的更多相关文章
- 【leetcode刷题笔记】Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...
- (python)leetcode刷题笔记05 Longest Palindromic Substring
5. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...
- (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 ...
- 【leetcode刷题笔记】Add Binary
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- leetcode刷题: 002 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
随机推荐
- EF Core 2.0中Transaction事务会对DbContext底层创建和关闭数据库连接的行为有所影响
数据库 我们先在SQL Server数据库中建立一个Book表: CREATE TABLE [dbo].[Book]( ,) NOT NULL, ) NULL, ) NULL, ) NULL, [Cr ...
- Linux中文件函数(二)
一.link.linkat.unlink.unlinkat.remove函数 创建一个指向现有文件的链接的方法是使用link函数或linkat函数.函数的原型为: #include <unist ...
- MySQL学习之视图的使用
视图基本操作 创建视图 视图的本质就是SQL指令(select语句) 基本语法:create view 视图名 as select 指令; 在这里的select指令可以是单表数据,也可以是连接查询. ...
- mysql启动错误处理
1.当启动MySQL时,报如下错误 [ERROR] Plugin 'InnoDB' init function returned error.[ERROR] Plugin 'InnoDB' regis ...
- git创建使用1https://blog.csdn.net/Hanani_Jia/article/details/77950594
这篇文章是我自己写的关于GitHub的内容,从我刚听到这个直到设置成功每一步都有详细的步骤来解释,其中有一些截图或者代码来自于网上. 首先,我先对GitHub来一个简单的介绍,GitHub有一个很强大 ...
- 微信小程序实现watch属性监听数据变化
Vue 提供了一种通用的方式来观察和响应 Vue 实例上的数据变动:监听属性 watch. 虽然watch的滥用会导致性能不佳,但在一些情况下我们还是需要watch,使得代码更加简洁.逻辑更加清晰(其 ...
- 网络文件系统nfs在ubuntu16.04的安装
1.搜索nfs-sudo apt-cache search nfs- 2.安装sudo apt-get install nfs-kernel-server 3.配置:/etc/exports /hom ...
- linux进程篇 (三) 进程间的通信3 IPC通信
3 IPC通信 用户空间 进程A <----无法通信----> 进程B -----------------|--------------------------------------|- ...
- leetcode add_binary 采坑记
尽管add_binary在leetcode中是一个简单难度,但是踩了不少坑,记录一下 描述: 给两个字符串形式的二进制数,要求求和并输出字符串形式的结果,其中a和b均不为空字符串 样例: a=“101 ...
- JavaScript基础part2
JavaScript对象 在JavaScript中除了null和undefined以外其他的数据类型都被定义成了对象,也可以用创建对象的方法定义变量,String.Math.Array.Date.Re ...