题意:

有两个链表,它们表示逆序的两个非负数。例 (2 -> 4 -> 3)表示342,求两个数字的和,并用同样的方式逆序输出。如342+465 = 807,你需要把结果表达为(7 ->0 ->8)。
 

思路:

模拟一下加法的运算过程,从个位开始加,进位保存下来,十位运算的时候把个位的进位加上,依次类推。
 

C++ Code

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode preHead(), *p = &preHead;
int extra = ;
while (l1 || l2 || extra) {
int sum = (l1 ? l1->val : ) + (l2 ? l2->val : ) + extra;
extra = sum / ;
p->next = new ListNode(sum % );
p = p->next;
l1 = l1 ? l1->next : l1;
l2 = l2 ? l2->next : l2;
}
return preHead.next;
}
};

Python Code

# 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
"""
p = preHead = ListNode(0)
extra = 0
while l1 or l2 or extra:
extra, val = divmod((l1 and l1.val or 0) + (l2 and l2.val or 0) + extra, 10)
p.next = ListNode(val)
p = p.next
if l1:
l1 = l1.next
if l2:
l2 = l2.next
return preHead.next

JS Code

/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var addTwoNumbers = function(l1, l2) {
var preHead = new ListNode(0)
var p = preHead
var extra = 0
while(l1 !== null || l2 !== null || extra !== 0) {
var sum = (l1 && l1.val || 0) + (l2 && l2.val || 0) + extra
extra = (sum > 9) ? 1 : 0
p.next = new ListNode(sum % 10)
p = p.next
l1 = l1 && l1.next || null
l2 = l2 && l2.next || null
}
return preHead.next
};

LeetCode 2. Add Two Numbers 解题报告的更多相关文章

  1. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  2. 【LeetCode】386. Lexicographical Numbers 解题报告(Python)

    [LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  3. LeetCode: Add Two Numbers 解题报告

    Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...

  4. LeetCode 728 Self Dividing Numbers 解题报告

    题目要求 A self-dividing number is a number that is divisible by every digit it contains. For example, 1 ...

  5. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  6. 【LeetCode】165. Compare Version Numbers 解题报告(Python)

    [LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  7. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  8. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  9. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

随机推荐

  1. iOS 项目上线流程

    一.证书的导出      1.1 前期工作        首先你需要有一个苹果的开发者帐号,一个Mac系统.        如果没有帐号可以在打开http://developer.apple.com/ ...

  2. jQuery原型技术分解

    jQuery原型技术分解 起源----原型继承 用户过javascript的都会明白,在javascript脚本中到处都是 函数,函数可以归置代码段,把相对独立的功能封闭在一个函数包中.函数也可以实现 ...

  3. HTML最基础的入门(上)

    一.互联网原理 互联网原理:上网即请求数据. 过程:在本机计算机浏览器上输入网址,发送一个http请求到服务器端,服务器会根据协议作出响应,将对应的网页文件通过http协议再传输给我们本地计算机,将网 ...

  4. mfix模拟流化床燃烧帮助收敛的方法

    1.在反应速率里用rate_limit函数:2.初始床料中可以添加一部分碳和灰.下面给出详细解释: 1.c3m生成的化学反应速率中有一个这样的函数: double precision function ...

  5. Varnish+Xcache构建高性能WEB构架初探

    本文主要讲述web优化方案和缓存工具的调研及使用.根据目前的测试结果来看,采用varnish+xcache作为 apache和 php缓存这种架构具有高并发.高稳定性,易扩展等优点,服务器的动态请求处 ...

  6. Canvas scale- 缩放

    可以进行坐标缩放,设为负值可以翻转图片: <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  7. 利用apache的mod_rewrite做URL规则重写

    使用mod_rewrite做url重写,伪静态,做过很多次,这次用几个例子记下来,便于后面查用. 使用方法: 1.在conf目录的httpd.conf文件中找到: LoadModule rewrite ...

  8. windows 8 安装 oracle 11g 报错:command line option syntax error,type command/? for help

    windows 8 安装 oracle 11g 报错:command line option syntax error,type command/? for help 在windows8操作系统上安装 ...

  9. Plugin execution not covered by lifecycle configuration的解决方案

    pom配置文件中,提示错误:Plugin execution not covered by lifecycle configuration. 如图: 这表示m2e在其执行maven的生命周期管理时没有 ...

  10. [JQuery]Jquery对象和dom对象

    jquery对象是jquery包装dom对象后产生的对象,它们都只能使用各自的方法. 1.定义变量时,通过$来区分: var $variable = jquery对象: var variable = ...