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. vue $forceUpdate() 强制重新渲染

    vue $forceUpdate() 强制重新渲染:https://blog.csdn.net/z9061/article/details/94862047

  2. java基础笔记(6)

    xml文件的写入 通过dom生成xml文件: package com.writexml; import java.io.File; import javax.xml.parsers.DocumentB ...

  3. [LeetCode] 82. 删除排序链表中的重复元素 II

    题目链接 : https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/ 题目描述: 给定一个排序链表,删除所有含有 ...

  4. Scrapy 教程(二)-操作流程

    scrapy 是个爬虫框架,是由python编写的,用法类似 django 框架. 创建工程 在开始爬虫之前,先创建工程 scrapy startproject projectname 目录结构如下图 ...

  5. asp.net后台cs中的JSON格式变量在前台Js中调用方法

    //后台cs代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using ...

  6. java复习(6)String、StringBuffer以及StringBuilder

    0.常见的编码表 ASC||:美国标准信息交换码,用一个字节的7位可以表示. ISO8859-1:拉丁码表.欧洲码表,用一个字节的8位来表示.无法存储汉字,或者只取了汉字的一半使用 GB2312:中文 ...

  7. CodeBlocks17.12+汉化包下载及用法

    本文已迁移至:https://blog.csdn.net/COCO56/article/details/95228780

  8. linux Nginx 的安装

    确保安装了 gcc,openssl-devel,pcre-devel,zilb-devel 下载官网:http://nginx.org/ [root@localhost tools]# wget ht ...

  9. php 多维数组转一维数组

    1.巧用 RecursiveIteratorIterator $k_arrays_arrays_tmp = iterator_to_array(new RecursiveIteratorIterato ...

  10. 测试员小白必经之路----常见的Dos命令

    Dos是什么? 是一个命令行执行的操作系统 进入终端: win + r 运行输入: cmd 当前计算机的本地时间:Time 退出当前正在执行的命令: ctrl +c 设置在多少时间后自动关机: Shu ...