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. c++primer chapter three

    3.1命名空间的using声明 using声明具有如下的形式:using namespace :: name; #include <iostream> using std :: cout; ...

  2. 【源码解读】pix2pix(一):训练

    源码地址:https://github.com/mrzhu-cool/pix2pix-pytorch 相比于朱俊彦的版本,这一版更加简单易读 训练的代码在train.py,开头依然是很多代码的共同三板 ...

  3. git LF will be replaced by CRLF in hellogit.txt

    这个是换行符的问题 参见:https://blog.csdn.net/starry_night9280/article/details/53207928

  4. spark(2)

    1.spark模块 -------------------------------------- (1)Spark Core //核心库 (2)Spark SQL //核心库 (3)Spark Str ...

  5. Python 通过wmi获取Window服务器硬件信息

    通过pip install wmi安装wmi 查看cpu序列号: wmic cpu get processorid 查看主板序列号: wmic baseboard get serialnumber 查 ...

  6. Nginx中配置https中引用http的问题

    Nginx中配置https中引用http的问题 遇到问题: 今天公司要在后台增加直播入口,使用腾讯云的实时音视频,要求是必须使用https,在配置完强制跳转https候,发现后台无法上传图片,在浏览器 ...

  7. BZOJ 2560: 串珠子 (状压DP+枚举子集补集+容斥)

    (Noip提高组及以下),有意者请联系Lydsy2012@163.com,仅限教师及家长用户. 2560: 串珠子 Time Limit: 10 Sec Memory Limit: 128 MB Su ...

  8. java File获取字节流

    /*文件64位编码*/ public static void main(String[] args) { byte[] fileByte = toByteArray(newFile); String ...

  9. php内置函数分析之array_change_key_case()

    PHP_FUNCTION(array_change_key_case) { zval *array, *entry; zend_string *string_key; zend_string *new ...

  10. python基本数据类型常用方法

    python基本数据类型 1.整型 1.1 int 1.2 bit_lenght # 当前数字的二进制位数,至少用n位表示 r = age.bit_length() >>> a = ...