原题:

You are given two linked lists representing two non-negative numbers.

The digits are stored in reverse order and each of their nodes contain a single digit

Add the two numbers and return it as a linked list

Input: (2->4->3) + (5->6->4)

Output: 7->0->8

代码如下,递归调用add函数即可:

class Solution:
# @param {ListNode} l1
# @param{ListNode}l2
# return {ListNode} def addTwoNumbers(self, l1, l2):
return self.add(l1, l2)
def add(self, l1, l2, s=0):
l = ListNode(s)
if l1:
l.val += l1.val
l1 = l1.next
if l2:
l.val += l2.val
l2 = l2.next
s= l.val/10
l.val = l.val % 10
if l1 or l2 or s: l.next = self.add(l1, l2, s)
return l

Leetcode 解题 Add Two Numbers Python的更多相关文章

  1. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

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

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

  3. LeetCode:1. Add Two Numbers

    题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...

  4. [Leetcode Week15] Add Two Numbers

    Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...

  5. LeetCode 面试:Add Two Numbers

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

  6. LeetCode #002# Add Two Numbers(js描述)

    索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...

  7. LeetCode 2. add two numbers && 单链表

    add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...

  8. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

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

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

随机推荐

  1. Kinect开发学习笔记之(一)Kinect介绍和应用

    Kinect开发学习笔记之(一)Kinect介绍和应用 zouxy09@qq.com http://blog.csdn.net/zouxy09 一.Kinect简单介绍 Kinectfor Xbox ...

  2. linux+nginx+tomcat负载均衡,实现session同步

    linux+nginx+tomcat负载均衡,实现session同步 花了一个上午的时间研究nginx+tomcat的负载均衡测试,集群环境搭建比较顺利,但是session同步的问题折腾了几个小时才搞 ...

  3. SQL Server数据库同步SQL

    select table_name,COLUMN_NAME,data_TYPE,CHARACTER_OCTET_LENGTH,CHARACTER_MAXIMUM_LENGTH from informa ...

  4. 在Linux下查看系统版本信息命令总结

    每次在想查看系统是多少位的时候.总是记不清究竟用哪个命令.所以做个总结. vonzhou@de16-C6100:~$ lsb_release -a No LSB modules are availab ...

  5. Qt树形控件QTreeView使用1——节点的添加删除操作 复选框的设置

    QtreeView是ui中最常用的控件,Qt中QTreeWidget比QTreeView更简单,但没有QTreeView那么灵活(QTreeWidget封装的和MFC的CTreeCtrl很类似,没有m ...

  6. C# - 系统类 - Math类

    Math类 ns:System 此类除了提供了最基本的运算功能 还提供了三角.指数.超常的计算 它的所有方法都是静态的 Math类的字段 E 常量e 自然对数底 值为2.71828182845905 ...

  7. iOS之获取当前时间日期并按固定格式显示

    写一个常用的获取当前日期,时间的代码.并且能以规定的格式显示出来 1 2 3 4 5 NSDate *currentDate = [NSDate date];//获取当前时间,日期 NSDateFor ...

  8. 需要设置jdk的三处位置:

    需要设置jdk的三处位置:1.tomcat需要一个JDK : Windows--->Preferences--->MyEclipse--->Servers--->Tomcat- ...

  9. Java ==,equals() 和hashCode

    Kruger上课讲到==和equals()方法是不同的,经过查询将具体内容整理一下,在查询过程中发现hashCode()方法与equlas()联系紧密,故一起研究. 比较浅显,以后如果理解更多随时更新 ...

  10. linux 常用查找命令 小技巧

    wc -l `find . -name "*.css"`|tail -n1 指定目录下 在指定后缀文件 查找关键字 find ./ -name "*" -exe ...