# 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
"""
cur = None
carry = 0
sum = 0
while l1 or l2 or carry: sums = carry
if l1:
sums += l1.val
l1 = l1.next
if l2:
sums += l2.val
l2 = l2.next if sums >= 10:
carry = 1
sums %=10
else:
carry = 0
item = ListNode(sums)
if cur == None:
cur = item
else:
p = cur
while p.next != None:
p = p.next
p.next = item return cur

leetcode add two numbers python的更多相关文章

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

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

  2. [LeetCode] Add Two Numbers 两个数字相加

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

  3. LeetCode Add Two Numbers II

    原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...

  4. LeetCode: Add Two Numbers 解题报告

    Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...

  5. Leetcode:Add Two Numbers分析和实现

    Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...

  6. [LeetCode] Add Two Numbers题解

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

  7. Leetcode 解题 Add Two Numbers Python

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

  8. Leetcode2:Add Two Numbers@Python

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

  9. [Leetcode] Add two numbers 两数之和

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

随机推荐

  1. find the safest road

    find the safest road Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Othe ...

  2. Expect:100-Continue & HTTP 417 Expectation[转]

    Expect:100-Continue & HTTP 417 Expectation 背景:今天调试火车票查询的代码,发现一个奇怪的事情,如果使用公司本地的代理,那么一切正常,如果使用的是公司 ...

  3. HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)

    HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...

  4. C# GridView弹出窗口新增行 删除行

    <%@ Page Language="C#" AutoEventWireup="true" EnableViewState="true" ...

  5. Dell 2950服务器CPU-E1422错误解决方法

    .造成原因:CPU松动或者是硅胶损耗 .解决方法: .断掉电源,将其后盖打开(在手没有静电的情况下操作) .拔掉周围的排热扇 .按住关卡,将其CPU卸下:并使用清洁剂清理,再次给CPU上涂上硅胶(均匀 ...

  6. 在IOS开发中,属性名为id的处理方法

    在.h 文件中定义属性名为id { int _id; } @property (nonatomic, assign) int id; 在.m 文件中用synthesize声明该属性,会自动生成get和 ...

  7. Oracle错误ORA-03113: end-of-file on communication channel处理办法

    oracle不能启动了,报错ORA-03113: end-of-file on communication channel (通信通道的文件结尾) 解决办法: SQL> startup ORAC ...

  8. QF——UI之几种常用的隐藏键盘的方法

    怎么在填写完UITextField之后,点击空白处,隐藏软键盘. 下面两个方法都可以隐藏键盘 [tf resignFirstResponder]; 停止textfield的第一响应者 [self.vi ...

  9. JPA 2.1实例(hibernate 实现)

    1.环境准备 1)java se 7 2)maven 3 3)mysql database 2.创建数据库和表结构 首先创建数据库.创建数据库脚本如下: create database jpa; 创建 ...

  10. hadoop笔记之MapReduce的应用案例(利用MapReduce进行排序)

    MapReduce的应用案例(利用MapReduce进行排序) MapReduce的应用案例(利用MapReduce进行排序) 思路: Reduce之后直接进行结果合并 具体样例: 程序名:Sort. ...