题目

You are given two linked lists representing two non-negative numbers. 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.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

代码

/**
* 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 dummy(-);
ListNode *p = &dummy,*p1 = l1,*p2 = l2;
int carry = ;
while(p1!=NULL || p2!=NULL){
const int v1 = p1==NULL?:p1->val, v2 = p2==NULL?:p2->val, v = (v1+v2+carry)%;
carry = (v1+v2+carry)/;
p->next = new ListNode(v);
p = p->next;
p1 = p1==NULL ? NULL : p1->next;
p2 = p2==NULL ? NULL : p2->next;
}
if ( carry > ) p->next = new ListNode(carry);
return dummy.next;
}
};

Tips

核心在于判断while停止条件:直到l1和l2都走完了才退出;如果l1或者l2先走完了,就当该位是0。

上面这种思路的好处是可以简化代码。

=========================================

第二次过这道题,已经对思路比较熟悉了;指针定义操作什么的,稍微想了一下;代码还是一次AC。

/**
* 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) {
int carry = ;
ListNode* pre = new ListNode();
ListNode* head = pre;
while ( l1 || l2 )
{
int digit1 = l1 ? l1->val : ;
int digit2 = l2 ? l2->val : ;
int curr_val = (digit1 + digit2 + carry)%;
carry = (digit1 + digit2 + carry)/;
if ( l1 ) l1 = l1->next;
if ( l2 ) l2 = l2->next;
pre->next = new ListNode(curr_val);
pre = pre->next;
}
if ( carry> ) pre->next = new ListNode(carry);
return head->next;
}
};

【Add Two Numbers】的更多相关文章

  1. leetcode 【 Add Two Numbers 】 python 实现

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  2. CF401D 【Roman and Numbers】

    题意将n(n<=10^18)的各位数字重新排列(不允许有前导零)  求  可以构造几个mod m等于0的数字解法状压f[S][k] 表示选用的位数集合为S,mod m 为k的方案数注意不能有前导 ...

  3. 【Mac系统 + Git】之上传项目代码到github上以及删除某个文件夹

    之前做开发的时候,用过一段时间git代码管理工具,用命令行操作感觉十分高大上,今天我想从头总结一篇Mac系统下如何利用git上传代码到github上的学习. 目录 一.安装Git 二.创建.ssh文件 ...

  4. 【LeetCode445】 Add Two Numbers II★★

    题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...

  5. 2. Add Two Numbers【medium】

    2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...

  6. 【LeetCode】2、Add Two Numbers

    题目等级:Medium 题目描述:   You are given two non-empty linked lists representing two non-negative integers. ...

  7. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  8. 【LeetCode-面试算法经典-Java实现】【002-Add Two Numbers (单链表表示的两个数相加)】

    [002-Add Two Numbers (单链表表示的两个数相加)] 原题 You are given two linked lists representing two non-negative ...

  9. 【总文档】rac增加新节点的方法步骤 How to Add Node/Instance or Remove Node/Instance in 10gR2, 11gR1, 11gR2 and 12c Oracle Clusterware and RAC

    [总文档]How to Add Node/Instance or Remove Node/Instance in 10gR2, 11gR1, 11gR2 and 12c Oracle Clusterw ...

随机推荐

  1. (转载)C#线程优先级详解

    计算机中经常会有多个任务同时运行,其中总有一些看起来更紧急,更需要优先完成.比如我们现在有两个任务,一个任务是下载一部电影,另一个任务是检测用户的输入.显然及时响应用户操作应具有更高的优先级,因为我们 ...

  2. C#访问修改符

    修饰符可以指定访问的可见性,还可以指定其本质.(文章摘自<C#高级编程(第9版)>以及Microsoft) 1. 可见性修饰符 public:应用于所有类型或成员,即任何代码均可以访问该项 ...

  3. 使用Eclipse连接SAP云平台上的HANA数据库实例

    SAP云平台(Cloud Platform)上的HANA数据库实例有两种方式访问: 1. 通过SAP云平台的基于网页版的Development Tool:SAP HANA Web-Based Deve ...

  4. IOS 绘制图片水印(封装)

    - (void)viewDidLoad { [super viewDidLoad]; // -1.加载图片 // UIImage *image = [UIImage imageNamed:@" ...

  5. 项目开发中dev、test和prod是什么意思

    开发环境(dev):开发环境是程序猿们专门用于开发的服务器,配置可以比较随意,为了开发调试方便,一般打开全部错误报告. 测试环境(test):一般是克隆一份生产环境的配置,一个程序在测试环境工作不正常 ...

  6. 用virtualenv构建一个新的python环境,这个新的环境在这个创建的文件夹下

    http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143271210830032 ...

  7. 【转】转自微信公众号 JavaScript 复杂判断的更优雅写法

    与微信公众号看到一篇js复杂判断的文章,对我启发很大,故转到博客园以供后期不断学习并应用于项目.原文地址:https://mp.weixin.qq.com/s/ClFDRj4MnAxv1dJ5VWKS ...

  8. Python02 变量

    变量 因为Python是弱变量类型编程语言,所以变量赋值不需要类型声明. 每个变量在内存中创建,都包括变量的标识,名称和数据这些信息. 每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建. 变量 ...

  9. SpringBoot学习1:创建第一个SpringBoot项目

    一.新建项目 二.打开项目的pom文件,在里面添加maven依赖 <!--springboot项目依赖的父项目--> <parent> <groupId>org.s ...

  10. python的对数

    python的对数 首先要导入 math 模块: import math import numpy as np math.log(8,2),此为以2为底8的对数 等于 math.log2(8); 等于 ...