蜗牛慢慢爬 LeetCode 2. Add Two Numbers [Difficulty: Medium]
题目
You are given two non-empty linked lists representing two non-negative integers. 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.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
翻译
给定两个非空的链表,表示两个非负整数。数字倒序存储,每个节点包含一个数字。将两个数字求和并将结果以链表形式返回
你可以假定数字不包含前导0(除了0本身之外)
输入:(2 - > 4 - > 3)+(5 - > 6 - > 4)
输出:7 - > 0 - > 8
Hints
Related Topics: Linked List, Math
注意简化代码
代码
Java
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode c1 = l1;
ListNode c2 = l2;
ListNode s = new ListNode(0);
ListNode d = s;
int sum = 0;
while(c1!=null||c2!=null){
sum /= 10;
if(c1!=null){
sum += c1.val;
c1 = c1.next;
}
if(c2!=null){
sum += c2.val;
c2 = c2.next;
}
d.next = new ListNode(sum%10);
d = d.next;
}
if(sum/10==1)
d.next = new ListNode(1);
return s.next;
}
}
Python
# 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
"""
out = 0
result = ListNode(0)
l = result
while l1!=None or l2!=None:
x1 = l1.val if l1!=None else 0
x2 = l2.val if l2!=None else 0
l.next = ListNode((x1+x2+out)%10)
out = (x1+x2+out)/10
l = l.next
l1 = l1.next if l1!=None else l1
l2 = l2.next if l2!=None else l2
if out!=0:
l.next = ListNode(out)
return result.next
//better solution from discuss
class Solution(object):
def addTwoNumbers(self, l1, l2):
carry = 0
root = n = ListNode(0)
while l1 or l2 or carry:
v1 = v2 = 0
if l1:
v1 = l1.val
l1 = l1.next
if l2:
v2 = l2.val
l2 = l2.next
carry, val = divmod(v1+v2+carry, 10)
n.next = ListNode(val)
n = n.next
return root.next
蜗牛慢慢爬 LeetCode 2. Add Two Numbers [Difficulty: Medium]的更多相关文章
- 蜗牛慢慢爬 LeetCode 5.Longest Palindromic Substring [Difficulty: Medium]
题目 Given a string s, find the longest palindromic substring in s. You may assume that the maximum le ...
- 蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]
题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...
- 蜗牛慢慢爬 LeetCode 1.Two Sum [Difficulty: Easy]
题目 Given an array of integers, return indices of the two numbers such that they add up to a specific ...
- 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]
题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 蜗牛慢慢爬 LeetCode 15. 3Sum [Difficulty: Medium]
题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...
- 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]
题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
随机推荐
- 每天一个Linux命令之less
之前一下子看过好多Linux命令,当初记得但是一直没有使用就忘了,现在仿这别人写一下争取能记得时间久一点233333 我使用的是ubuntu Less 这是一个查看文件的命令 进行翻页的命令有一下几个 ...
- scala (4) 可变数组和不可变数组
在scala中数组分为不可变长数组(在immutable包下)和可变长数组(在mutable包下) 不可变长数组指的是长度不可变,但是数组中角标对应的元素的值是可变的 可变数组指的是长度和数组中角标对 ...
- spark 例子count(distinct 字段)
spark 例子count(distinct 字段) 例子描述: 有个网站访问日志,有4个字段:(用户id,用户名,访问次数,访问网站) 需要统计: 1.用户的访问总次数去重 2.用户一共访问了多少种 ...
- 20155332 2016-2017-2 《Java程序设计》第10周学习总结
20155332 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 了解计算机网络基础 掌握Java Socket编程 理解混合密码系统 掌握Java 密码技 ...
- centos7安装cacti
参考博客地址:https://blog.csdn.net/kenn_lee/article/details/80565385 Cacti是一套基于PHP,MySQL,SNMP及RRDTool开发的网络 ...
- Tomcat安装部署和安全加固优化以及反向代理应用
1.Tomcat介绍 Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun和其他一些公司及个人共同 ...
- Siki_Unity_3-8_Lua编程(未完)
Unity 3-8 Lua编程 任务1&2&3:前言 课程内容: Lua从入门到掌握 为之后的xLua和其他热更新方案打下基础 任务4:Lua简介 Lua是轻量小巧的脚本语言--无需编 ...
- 一学就会pip换镜像源
首先介绍一个国内好用的镜像站 阿里云 http://mirrors.aliyun.com/pypi/simple/ 豆瓣 http://pypi.douban.com/simple/ 清华大学 htt ...
- hadoop常见错误解决方法
一.启动集群时 1.节点启动失败 1.1端口占用 1.1报错信息:address already in use - bind Address:50070 解决步骤: 查询端口占用:lsof -i:50 ...
- Harbor配置https,并安装内容信任插件(notary)
1.配置https https://github.com/goharbor/harbor/blob/master/docs/configure_https.md 2.harbor安装notary插件 ...