leetcode-mid-Linked list-2 Add Two Numbers
mycode 87.22%
# Definition for singly-linked list.
# class ListNode(object):
# 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
"""
dummy = link = ListNode(-1)
add = 0
while l1 and l2:
temp = l1.val + l2.val + add
if temp >= 10:
link.next = ListNode(temp%10)
add = temp // 10
else:
add = 0
link.next = ListNode(temp)
print(temp)
l1 = l1.next
l2 = l2.next
link = link.next
l1 = l1 or l2
while l1:
temp = l1.val + add
if temp >= 10:
link.next = ListNode(temp%10)
add = temp // 10
else:
add = 0
link.next = ListNode(temp)
print(temp)
l1 = l1.next
link = link.next
if add:
link.next = ListNode(add)
link = link.next
link.next = None
return dummy.next
参考:
1、如何把其中一个为None放到while里面去?
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
dummy = link = ListNode(-1)
add = 0
while l1 or l2:
res = 0
if not l2: l2 = ListNode(0)
if not l1:
l1 = ListNode(0)
temp = l1.val + l2.val + add
print('temp...',temp+add)
if temp > 9:
res = temp % 10
add = temp // 10
#print('if...',res,add)
else:
res = temp
add = 0
#print('else...',res,add)
l1 = l1.next
l2 = l2.next
dummy.next = ListNode(res)
dummy = dummy.next
if add > 0:
print('addd...')
dummy.next = ListNode(add)
dummy = dummy.next
dummy.next = None
return link.next
2、如何把进位也放进去?
class Solution(object):
def addTwoNumbers(self, l1, l2):
dummy = cur = ListNode(0)
curry = 0
while l1 or l2 or curry:
if l1:
curry = curry+l1.val
l1 = l1.next
if l2:
curry = curry+l2.val
l2 = l2.next
cur.next = ListNode(curry%10)
cur = cur.next
curry = curry//10
return dummy.next
5. Longest Palindromic Substring
leetcode-mid-Linked list-2 Add Two Numbers的更多相关文章
- leetcode刷题: 002 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode 2. 两数相加(Add Two Numbers)
2. 两数相加 2. Add Two Numbers 题目描述 You are given two non-empty linked lists representing two non-negati ...
- LeetCode第四题,Add Two Numbers
题目原文: You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- 【Leetcode】【Medium】Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- (python)leetcode刷题笔记 02 Add Two Numbers
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...
- 【leetcode刷题笔记】Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【LeetCode每天一题】Add Two Numbers(两链表相加)
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- LeetCode.2-两个数字相加(Add Two Numbers)
这是悦乐书的第340次更新,第364篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第1题(顺位题号是2).给定两个非空链表,表示两个非负整数. 数字以相反的顺序存储, ...
- LeetCode(2):Add Two Numbers 两数相加
Medium! 题目描述: 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头 ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
随机推荐
- java 异常体系详细介绍
一.异常概述与异常体系结构 异常:在Java语言中,将程序执行中发生的不正常情况称为"异常".(开发过程中的语法错误和逻辑错误不是异常). Java把异常当作对象来处理,并定义一个 ...
- NoSQL特点
- P2220 [HAOI2012]容易题
传送门 首先 $(\sum_{i=1}^{n}a_i)(\sum_{i=1}^{m}b_i)$ 展开以后包含了所有 $ab$ 两两相乘的情况并且每种组合只出现一次 发现展开后刚好和题目对序列价值的定义 ...
- mongodb启动报错,child process failed, exited with error number 1
error: child process failed, exited with error number 1 第一次安装mongodb,随后启动一般不会出现上面的错误,出现这种错误的原因一般是mon ...
- JSP中九大内置对象及其作用
jsp中有九大内置对象分别为:request,response,session,application,out,pageContext,page,config,exception. request:请 ...
- 一般软件开发流程和BBS表设计
项目开发流程 需求分析 架构师+产品经理+开发组组长 和客户公司谈需求之前 ,事先需要想一下这个项目要怎么做 里面的坑点提前想好比较简单的解决方案 在跟客户谈的时候有意识的引导客户朝你已经想好的方案上 ...
- Nginx 的全局和虚拟主机配置
Httpd.conf nginx.conf my-heavy-innode-4G.cnf php.ini 用中文注释 # user:指定 Nginx Worker 进程运行用户和用户组,默认 nob ...
- ElasticSearch - 解决ES的深分页问题 (游标 scroll)
https://www.jianshu.com/p/f4d322415d29 1.简介 ES为了避免深分页,不允许使用分页(from&size)查询10000条以后的数据,因此如果要查询第10 ...
- 北京太速科技股份有限公司产品手册V201903020
如果您无法正常查看,请点击在线浏览 如果您无法正常查看,请点击在线浏览 了解更多产品信息,请扫描二维码,期待您的关注 ...
- DMA方式的数据传送过程
DMA方式具有如下特点: 1. 外部设备的输入输出请求直接发给主储存器. 主存储器既可以被CPU访问,也可以被外围设备访问.因此,在主存储器中通常要有一个存储管理部件来为各种访问主存储器的申请排队 ...