题目

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. Zeta--S3 Linux抓取一帧YUV图像后使用硬件编码器编码成H.264

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <getopt.h&g ...

  2. IDEA常见错误解决

    tomcat控制台乱码 在tomcat的edit configurations里加入参数:-Dfile.encoding=UTF-8   导入的项目在重写时报 @Override is not all ...

  3. 20155207 《Java程序设计》实验报告二:Java面向对象程序设计

    实验要求 1.初步掌握单元测试和TDD 2.理解并掌握面向对象三要素:封装.继承.多态 3.初步掌握UML建模 4.熟悉S.O.L.I.D原则 5.了解设计模式 实验内容 一.单元测试 1.三种代码 ...

  4. 20155310 2016-2017-2《Java程序设计》课程总结

    20155310 2016-2017-2<Java程序设计>课程总结 (按顺序)每周作业链接汇总 预备作业一:对师生关系的看法以及对专业的期望 预备作业二:Learning by doin ...

  5. 20155338 2016-2017-2 《Java程序设计》第10周学习总结

    20155338 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 网络编程 · 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事 ...

  6. 《Java 程序设计》课堂实践项目-命令行参数

    <Java 程序设计>课堂实践项目 课后学习总结 目录 改变 命令行参数实验要求 课堂实践成果 课后思考 改变 修改了博客整体布局,过去就贴个代码贴个图很草率,这次布局和内容都有修改.加了 ...

  7. primary key和unique的区别

    定义了 UNIQUE 约束的字段中不能包含重复值,可以为一个或多个字段定义 UNIQUE 约束.因此,UNIQUE 即可以在字段级也可以在表级定义, 在 UNIQUED 约束的字段上可以包含空值.OR ...

  8. day2 Ubuntu配置源

    1.寻找国内镜像源 https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/ 2.配置source  list源 sources.list系统自带的,源是来U ...

  9. day8 RHCE

    12 .实现一个 web 服务器在server0上配置一个站点http://server0.example.com,然后执行以下步骤: 从http://classroom.example.com/ma ...

  10. 【NOIP2018】提高组题解

    [NOIP2018]提高组题解 其实就是把写过的打个包而已 道路铺设 货币系统 赛道修建 旅行 咕咕咕 咕咕咕