# 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. ActionBar隐藏修改图标和标题

    有时候在一些子页面或者内容页面,不需要显示ActionBar的标题栏图标.可用如下方式进行设置. 首先获取到ActionBar对象 ActionBar actionBar=getActionBar() ...

  2. .net通用权限框架B/S (四)--DAL数据层以及数据接口

    数据层以及数据接口设计如下图(以g_orga组织机构和g_role角色)为例,这几个类可以通过.tt模版生成 设计参考学习http://www.cnblogs.com/hanyinglong/arch ...

  3. leetcode Linked List Cycle python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  4. mysql 全文检索的匹配问题

    开发过程中碰到一个关于mysql 全文检索的问题,控制台打印的SQL语句拿到数据库里执行,结果不对.后来发现原来是少了双引号.下面是网上找到的资料,我是看到这个才意识到自己的问题. 这是之前在数据库执 ...

  5. Docker容器的网络连接

    Docker容器的网络连接 Docker容器的网络连接 我们用ifconfig命令来查看网络设备 我们可以看到上面有个叫docker0的网络设备,docker守护进程就是通过docker0为docke ...

  6. Js把URL中的参数解析为一个对象

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8" /> <title&g ...

  7. js私有化属性

    我们先来看一个例子: var Demo1 = function(val){ this.value = val; this.getValue = function(){ return this.valu ...

  8. SQL Server Primary key 、clustered index 、 unique

    primary key: 1.主键不可以有空值. 2.不可以有重复行. unique : 1.可以有空行. 2.不可以有重复行. clustered index: 1.可以有重复行. 2.可以有空行. ...

  9. 使用gSoap做一个简单的CS系统

    首先介绍一下gSoap,这是一个非常厉害的网络库,封装程度非常高而且跨平台. 支持SSL链接,数据格式为XML. 介绍地址: http://sourceforge.net/projects/gsoap ...

  10. C语言入门(10)——if分支语句

    在我们写的函数中可以有多条语句,但这些语句总是从前到后顺序执行的.除了从前到后顺序执行之外,有时候我们需要检查一个条件,然后根据检查的结果执行不同的后续代码,在C语言中可以用分支语句实现,比如: if ...