蜗牛慢慢爬 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 ...
随机推荐
- vue相关理论知识
es6常用语法简介 es是js的规范标准 let 特点: 1.有全局和函数作用域,以及块级作用域(用{}表示块级作用域范围) 2.不会存在变量提升 3.变量不能重复声明 const 特点: 1.有块级 ...
- Bugku Crack it
知识点:kali john工具 不知道hashcat怎么样 一. 用winhex打开 发现右边有root:等信息,想到了linux系统,看了别人的wp,kali中有一个工具john可以破解密码 二. ...
- PyQt5 signal and slot
PyQt5 的 signal 与 slot 有所改变,例如,先定义一个 ZeroSignal 类: class ZeroSignal(QObject): atzero = pyqtSignal(int ...
- 【 C 】高级字符串查找之 strspn 和 strcspn 的思考
我的CSDN博客 strspn 这个库函数是真的很难理解,看了很多中文描述,反正都是不知所云,给出一系列的例子,结果也是让我瞠目结舌,荒唐.荒谬.荒诞! 特此记录理解过程,最后竟然是百度百科让我明白了 ...
- 20155203 实验一《Java开发环境的熟悉》实验报告
20155203 实验一<Java开发环境的熟悉>实验报告 一.实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 二.练 ...
- 【mysql经典题目】行转列
参考:http://www.cnblogs.com/h07061108/p/mysql_questions.html#3806338 实现如下效果 CREATE TABLE IF NOT EXISTS ...
- 五、利用EnterpriseFrameWork快速开发基于WebServices的接口
回<[开源]EnterpriseFrameWork框架系列文章索引> EnterpriseFrameWork框架实例源代码下载: 实例下载 前面几章已完成EnterpriseFrameWo ...
- html查漏补缺之meta标签
什么是meta标签? meta标签是html标记head区的一个关键标签,它位于HTML文档的<head>和<title>之间(有些也不是在<head>和<t ...
- Ubuntu 16.04 主题美化及常用软件安装
一.主题美化 系统清理 系统更新: 安装完系统之后,需要更新一些补丁.Ctrl+Alt+T调出终端,执行一下代码: sudo apt-get update sudo apt-get upgrade 卸 ...
- 2018百度之星开发者大赛-paddlepaddle学习
前言 本次比赛赛题是进行人流密度的估计,因为之前看过很多人体姿态估计和目标检测的论文,隐约感觉到可以用到这次比赛上来,所以趁着现在时间比较多,赶紧报名参加了一下比赛,比赛规定用paddlepaddle ...