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的更多相关文章

  1. leetcode刷题: 002 Add Two Numbers

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

  2. LeetCode 2. 两数相加(Add Two Numbers)

    2. 两数相加 2. Add Two Numbers 题目描述 You are given two non-empty linked lists representing two non-negati ...

  3. LeetCode第四题,Add Two Numbers

    题目原文: You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  4. 【Leetcode】【Medium】Add Two Numbers

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

  5. (python)leetcode刷题笔记 02 Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

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

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

  7. 【LeetCode每天一题】Add Two Numbers(两链表相加)

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  8. LeetCode.2-两个数字相加(Add Two Numbers)

    这是悦乐书的第340次更新,第364篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第1题(顺位题号是2).给定两个非空链表,表示两个非负整数. 数字以相反的顺序存储, ...

  9. LeetCode(2):Add Two Numbers 两数相加

    Medium! 题目描述: 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头 ...

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

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

随机推荐

  1. phpstudy添加PHP

    想在phpstudy2018里面增加一个php版本,操作如下: 一.下载php-7.2.19-ts文件,解压缩,放在相应的目录下: 二.修改Apache的配置文件1.修改httpd.conf 配置,D ...

  2. C++中的const分析

    1,C 语言中的 const: 1,const 修饰的变量是只读的,本质还是变量: 1,C 语言中的 const 使变量具有只读属性: 2,const 只在编译期有用,在运行期无用: 3,const ...

  3. 嵌入式软件工程师C语言经典笔试2

    1. 使用宏定义swap函数,不使用中间变量 #define swap(x,y) {(x) = (x) + (y);(y) = (x) - (y);(x) = (x) - (y)} 2. 实现字符串的 ...

  4. Views的补充

    views的补充 请求头一般与请求内容用/r/n/r/n隔开 请求头包含的内容 request.Meta(...) 一般在下面几种方法里面取不到的东西需要去原生的头里面去取,比如用户的终端类型 req ...

  5. ThinkPHP关联模型如何关联非主键

    ThinkPHP关联模型默认是主键外键关联 官方并没有提供相关文档 如何实现非主键与非主键间之间的关联 <?php namespace Admin\Model; use Think\Model\ ...

  6. PowerEdge T630服务器安装机器学习环境(Ubuntu18.04、Nvidia 1080Ti驱动、CUDA及CUDNN安装)

    安装步骤 在开始安装之前,我要说明一下,这个Ubuntu18.04系统的安装,使用的连接线(就是服务器和电脑显示器的连接线)必须两头都是VGA连接线,不能使用VGA转HDMI连接线,也不能用DVI转D ...

  7. msdn帮助,离线下载

    这是我在msdn下载,如果要看msdn帮助,不是在线看就是visual studio 帮助那下载. 在网速不好的时候msdn看,会让人不爽. 帮助那个下载速度很慢,于是我就去下载离线. 因为微软看不到 ...

  8. MOVE - 重定位一个游标

    SYNOPSIS MOVE [ direction { FROM | IN } ] cursorname DESCRIPTION 描述 MOVE 在不检索数据的情况下重新定位一个游标. MOVE AL ...

  9. 4Linux 终端命令格式

    Linux 终端命令格式 转自 目标 了解终端命令格式 知道如何查阅终端命令帮助信息 01. 终端命令格式 command [-options] [parameter] 说明: command:命令名 ...

  10. 写api接口神器--带你5分钟了解swagger

    随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染,变成了:前端渲染.先后端分离的形态,而且前端技术和后端技术在各自的道路上越走越远. 前端和后端的唯一联系,变成了API接口:API文档变成了 ...