Leetcode 解题 Add Two Numbers Python
原题:
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的更多相关文章
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- LeetCode:1. Add Two Numbers
题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...
- [Leetcode Week15] Add Two Numbers
Add Two Numbers 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/add-two-numbers/description/ Descrip ...
- LeetCode 面试:Add Two Numbers
1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- LeetCode #002# Add Two Numbers(js描述)
索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...
- LeetCode 2. add two numbers && 单链表
add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
- [LeetCode] 2. Add Two Numbers 两个数字相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
随机推荐
- MySQL bug:server-id默认被自己主动置为1
昨天同事在做主从时,从库报例如以下错误: Got fatal error 1236 from master when reading data from binary log: 'Misconfigu ...
- -bash: ls: command not found
在iMac下ls既然command not found,查找了下 原因:在设置环境变量时,编辑profile文件没有写正确,导致在命令行下 ls等命令不能够识别.解决方案: 直接在控制台下 expo ...
- java09 队列Queue与Deque
队列Queue与Deque. Enumeration Hashtable与Hashtable子类Properties(资源配置文件) 引用类型(强.软.弱.虚)与WeakHashMap Identit ...
- linux启动的过程
总结一下,linux的开机整个流程. · 1: 启动电源后,主机第一步先做的就是查询BIOS(全称:basic input/output system 基本输入输出系统)信息.了解整个系统的硬件状态, ...
- linux服务器上
命令行>mysql -uwin -pwin2009进入mysql command状态>use mindo时入mindo数据库>source 500sql.txt执行sql
- redis持久化(摘录)
redis是一个支持持久化的内存数据库,也就是说redis需要经常将内存中的数据同步到磁盘来保证持久化.redis支持两种持久化方式,一种是 Snapshotting(快照)也是默认方式,另一种是Ap ...
- Asp.Net分页存储过程
SQL分页语句 一.比较万能的分页: sql代码: 1 2 3 select top 每页显示的记录数 * from topic where id not in (select top (当前的 ...
- IP地址基础知识
IP地址基础知识 网络号:用于识别主机所在的网络:主机号:用于识别该网络中的主机. 一 OSI/RM模型 应用层 表示层 会话层 传输层 网络层 数据链路层 物理层 二 TCP/IP模型 数据链路层( ...
- semaphore(信号量)使用说明
例子: 以一个停车场运作为例.为了简单起见,假设停车场只有三个车位,一开始三个车位都是空的.这时如果同时来了五辆车,看门人允许其中三辆不受阻碍的进入,然后放下车拦,剩下的车则必须在入口等待, ...
- modelsim脚本文件的编写
第一章 ModelSim介 绍 本指南是为 ModelSim5.5f版本编写的,该版本运行于UNIX和Microsoft Windows 95/98/Me/NT/2000的操作系统环境中.本指南覆盖了 ...