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 ...
随机推荐
- Dubbo 序列化协议 5 连问,你接得住不?
1)dubbo 支持哪些通信协议? 2)支持哪些序列化协议? 3)说一下 Hessian 的数据结构? 4)PB 知道吗? 5)为什么 PB 的效率是最高的? 面试官心理分析 上一个问题,说说 dub ...
- SVN与Git的优点差异比较
今天自己还是很有进步的,但是 下午的进度很慢,学习还是得回去,不能在工位进行 在网上看到一篇有关于SVN与Git的区别 复制下来了,以后可以经常看看 一. 集中式vs分布式 1. Subversion ...
- 逆向工程 生成mapper 接口的 重要方法
@Test public void testSelectByExample() { ItemsExample itemsExample = new ItemsExample(); ItemsExamp ...
- 【6.10校内test】T3 加分二叉树
加分二叉树[题目链接] 感觉我超废 这道题当时压根就不会qwq(我倒是挺适合写rand的qwq) 对于暴力的做法: 输入数据,定义数组men[i][i]=v[i](输入的第二行): dfs: dfs ...
- viewset的使用的方法
viewset的使用方法中是怎么区分的update和crate modelViewSet中设置了这个类之后,设置了类成员变量:queryset 和 seriazlier class,所以POST操作直 ...
- SCUT - 216 - 宝华科技树
https://scut.online/p/216 演员 把这个当成dp算了半天,各种姿势,好吧,就当练习一下树dp. 假如是每个节点的层数之和,按照dp[i][j]为从i点出发获得j科技的最小费用d ...
- Chrome浏览器记住密码后input框黄色背景且背景图片不显示的问题
Chrome浏览器记住密码后再进入登录页后,输入框背景颜色变为黄色,字体颜色变成#000黑色,且添加的背景图片也那不显示了,进入审查元素后,发现浏览器默认给输入框添加了样式,并且无法通过importa ...
- (新手入门,学习笔记)通过NPM进行Vue.js的安装
NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,本文只介绍如何通过NPM进行安装Vue.js NodeJS官方网站:http://nodejs.cn/downlo ...
- socket 服务器向指定的客户端发消息
一.需求 需求如题. 当多个客户端连接服务器时,服务器如何给指定的客户端发送消息. 二.解决方案 核心思想: 在服务器端,需保存不同客户端的socket列表及客户端相关信息. socket含有发送方和 ...
- Use of Function Arctan
Use of Function Arctan Time Limit:10000MS Memory Limit:0KB 64bit IO Format:%lld & %llu S ...