leetcode-【中等题】2. Add Two Numbers
题目
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
链接
https://leetcode.com/problems/add-two-numbers/
答案
1、直接按顺序加就行,保存进位carry
2、到最后还需要考虑进位carry是否为0
代码
/**
* 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) {
if(l1 == NULL)
{
return l2;
} if(l2 == NULL)
{
return l1;
} ListNode *ans = NULL;
ListNode *point = NULL;
int carry = ;
int sum = ;
while(l1 != NULL && l2 != NULL)
{
sum = carry + l1->val + l2->val;
carry = sum / ;
sum = sum % ; ListNode *value = new ListNode(sum);
if(ans == NULL)
{
ans = value;
} if(point != NULL)
{
point->next = value;
}
point = value; l1 = l1->next;
l2 = l2->next;
} while(l1 != NULL)
{
sum = carry + l1->val;
carry = sum / ;
sum = sum % ;
ListNode *value = new ListNode(sum);
point->next = value;
point = value; l1 = l1->next;
} while(l2 != NULL)
{
sum = carry + l2->val;
carry = sum / ;
sum = sum % ;
ListNode *value = new ListNode(sum);
point->next = value;
point = value; l2 = l2->next;
} if(carry != )
{
ListNode *value = new ListNode(carry);
point->next = value;
point = value;
} return ans;
}
};
leetcode-【中等题】2. Add Two Numbers的更多相关文章
- LeetCode第二题:Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- LeetCode刷题系列——Add Two Numbers
题目链接 这个题目很简单,归并而已,好久没练编程,居然忘了在使用自定义类型前,要进行初始化(new操作). class ListNode{ int val; ListNode next; ListNo ...
- LeetCode算法题-Sum of Square Numbers(Java实现)
这是悦乐书的第276次更新,第292篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第144题(顺位题号是633).给定一个非负整数c,判断是否存在两个整数a和b,使得a的 ...
- leetcode 中等题(1)
2. Add Two Numbers(中等) /** * Definition for singly-linked list. * struct ListNode { * int val; * Lis ...
- LeetCode解题笔记 - 2. Add Two Numbers
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...
- leetcode中等题
# Title Solution Acceptance Difficulty Frequency 1 Two Sum 44.5% Easy 2 Add Two Number ...
- LeetCode第四题,Add Two Numbers
题目原文: You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- 【LeetCode刷题系列 - 002题】Add Two Numbers
题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...
- 【LeetCode每天一题】Add Two Numbers(两链表相加)
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- LeetCode(2)Add Two Numbers
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
随机推荐
- MySql 的常用优化
1.选取最适用的字段属性 MySQL可以很好的支持大数据量的存取,但是一般说来,数据库中的表越小,在它上面执行的查询也就会越快.因此,在创建表的时候,为了获得更好的性能,我们可以将表中字段的宽度设得尽 ...
- cpp项目的组织
编译篇 较大型cpp项目的代码组织.编译都是深耦合的. 一般提供一个总体的makefile,进入各个模块,又有自己的makefile,这些makefile又都依赖于一些被include的文件的的定义, ...
- 之前想搞一个nim但因为是自用我会持续修复完善
异步方式的优点:客户端和服务端互相解耦,双方可以不产生依赖.缺点是:由于引入了消息中间件,在编程的时候会增加难度系数.此外,消息中间件的可靠性.容错性.健壮性往往成为这类架构的决定性因素. 幸运的是程 ...
- windows 下 webstorm 使用SVN
1.安装了webstorm之后,用了很久都没有配置SVN 现在想配置svn,结果发现一般的svn程序不好用. 经指导,发现需要安装一个专用于webstorm的SVN 2.在file->setti ...
- JS中正则匹配的三个方法match exec test的用法
javascript中正则匹配有3个方法,match,exec,test: match是字符串的一个方法,接收一个RegExp对象做为参数: match() 方法可在字符串内检索指定的值,或找到一个或 ...
- mybatis generator使用(基于maven)
1.添加maven依赖 <dependency> <groupId>org.mybatis.generator</groupId> <artifactId&g ...
- varchar2_to_blob,应用向数据库更新LOB字段时的超时问题
将字符串转换为BLOB类型数据,写入服务器. 1,首先利用to_clob函数把varchar2字段转成 clob字段. 2 利用c2b上面函数将clob转成blob. 即: c2b(to_clob( ...
- SSIS自定义数据流组件开发(血路)
由于特殊的原因(怎么特殊不解释),需要开发自定义数据流组件处理. 查了很多资料,用了不同的版本,发现各种各样的问题没有找到最终的解决方案. 遇到的问题如下: 用VS2015编译出来的插件,在SSDTB ...
- Python error: Unable to find vcvarsall.bat
在安装一些Python模块时,大部分是cpython写的模块时会发生如下错误 error: Unable to find vcvarsall.bat.先前的一篇文章:在Windows上安装Scrapy ...
- 前端 动态表单提交(post、put)
第一步:form表单定义统一属性 <input type="text" class="form-value" /> 第二步:获取所有值 var fo ...