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 ...
随机推荐
- c++虚表的使用 通过虚表调用虚函数的演示代码
//演示一下c++如何找到虚表地址vptr以及如何通过虚表调用虚函数 //zhangpeng@myhexin.com 20130811 #include <iostream> using ...
- mongodb地理位置索引
初始化集合(经度在前,纬度在后) ? 1 2 3 mongos> db.checkins.insert({ "_id" : "101", "lo ...
- MySQL 复制+快照恢复误删除操作实验测试
下面假定2个场景: 场景1:主从架构,没有延迟,某DBA误操作:drop database [复制+快照:在线备份]场景2:存在不确定性或者风险性较大的操作,如升级测试,大表变更[啥事都在快照上折腾, ...
- android开发入门经验 ADT Bundle环境搭建
现在有许多做开发的转做移动端开发,做J2EE的转做Android开发,我也把自己的一些入门经验与大家分享一下,希望能给你带来帮助. 工具/原料 JDK,ADT,JAVA 方法/步骤 开发工具的准备 ...
- VMWARE FUSION 6 KEY
Serial number: VZ15K-DKD85-M85EP-W4P79-XAAU4 Serial number: VU50A-2UW9Q-M88UY-D7MQX-ZG8X8 Serial num ...
- The 10 Most Important Security Controls Missing in JavaEE--reference
JavaEE has some excellent built-in security mechanisms, but they don’t come close to covering all th ...
- Beyond REST: How to build a HATEOAS API in Java with Spring MVC, Jersey (JAX-RS) and VRaptor
http://zeroturnaround.com/rebellabs/beyond-rest-how-to-build-a-hateoas-api-in-java-with-spring-mvc-j ...
- docker image export or import
docker save <image-name> docker load < <bak>.tar
- msql 按值排序
ORDER BY find_in_set(status,'705,710,706,1027,707,709,708'),create_time desc
- 1.Linux系统(CenntOS)固定IP的设定
首先:我们配置一个临时的固定的固定ip,作用是让我们用其他的shell工具连接虚拟机 ifconfig eth0 192.168.10.168 在主机用ping命令查询连通后再进行下一步 ping 1 ...