题目

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]的更多相关文章

  1. 蜗牛慢慢爬 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 ...

  2. 蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]

    题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...

  3. 蜗牛慢慢爬 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 ...

  4. 蜗牛慢慢爬 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 ...

  5. 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  6. 蜗牛慢慢爬 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 ...

  7. 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]

    题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  8. 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]

    题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  9. 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]

    题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

随机推荐

  1. 批处理之 for /f 中的delims和tokens

    0x00 前言 今天在对windows进行提权之前的系统信息收集的时候,需要使用到一条批处理语句把特定部分的内容从一个txt的文本当中提取出来:该条语句是如下: for /f "tokens ...

  2. 【Mac】解决「另一个活跃的 Homebrew 进程正在进行中」问题

    问题描述 在安装 tesseract 的语言包时,由于网络下载速度太慢,我按下 ctrl + z 退出了安装,当再次输入安装命令时,系统报错如下: 解决方法 使用以下命令删除 homebrew 进程锁 ...

  3. 直流电机驱动,TIMER口配置

    电机的电压输出能力和频率有关??? 修改前:------------------------------------------------------------------------------ ...

  4. Git 命令及使用经验

    手册中的基本命令: CONFIGURE TOOLING Configure user information for all local repositories $ git config --glo ...

  5. go VS NET 字符串操作能力

    今天拿golang 与 NET4.0 做了在字符串方面的性能比较,看看谁牛! 一.读取txt文本文件 GO的代码: readbuf, _ := ioutil.ReadFile(userFile) st ...

  6. Linux入门进阶第三天——软件安装管理(下)

    一.yum在线安装 之前的rpm包各种依赖性太强!安装复杂,yum的好处就来了: // yum 在redhat是付费服务 1.yum源文件 先进入到yum目录: 我们打开默认生效的Base包 2.光盘 ...

  7. 20155215 2006-2007-2 《Java程序设计》第2周学习总结

    20155215 2006-2007-2 <Java程序设计>第2周学习总结 教材学习内容总结 第三章主要讲述了JAVA程序编写中的一些基本语法.其实看了第三章之后我就感觉到,C语言不愧是 ...

  8. 20155218 2016-2017-2《Java程序设计》课程总结

    20155218 2016-2017-2<Java程序设计>课程总结 目录 每周作业链接汇总 实验报告链接汇总 博客中的经验与收获 代码托管 课堂项目实践 课程收获与不足 给开学初的你和学 ...

  9. 20155338 ch02 ch03课下作业

    20155338 ch02 ch03课下作业 要求: 1.补充完成课上测试(不能只有截图,要有分析,问题解决过程,新学到的知识点) 课上测试-3-ch02 1.编写一个程序 "week060 ...

  10. Ubuntu genymotion

    官网注册帐号 下载genymotion-[VERSION]_[ARCH].bin 进入android studio In Android Studio, go to File > Setting ...