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 ...
随机推荐
- jquery设置下拉菜单
jQuery代码 1,引用jQuery库 2,show方法 3,hide方法 <script type="text/javascript"> $function(){ ...
- [leetcode]Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- 【OPENGL】第三篇 着色器基础(二)
在这一小节,主要学习GLSL的基本数据类型以及控制结构.GLSL具备了C++和Java的很多特性,我们会先了解所有着色阶段共有的特性,再了解各个着色器的专属特性. 1.着色器的基本结构 一个着色器程序 ...
- Apache-Jemeter web性能测试工具使用
Jmeter是一款java开源的性能测试软件. 要使用该工具进行性能测试,首先需要下载该工具到你的电脑,接着配置java开发环境以及Jmeter环境.搭建完成之后,OK,我们就可以进行测试了. 测试第 ...
- C#导出Excel,并设置简单格式
protected void ExportExcel(DataTable dt) { string fileName = “FileName”; Microsoft.Office.Interop.Ex ...
- python logging模块详解[转]
一.简单将日志打印到屏幕: import logging logging.debug('debug message') logging.info('info message') logging.war ...
- linux find命令
Linux中find常见用法示例 ·find path -option [ -print ] [ -exec -ok command ] {} \; find命令的参数 ...
- svn提交报e200007错误
org.apache.subversion.javahl.ClientException: svn: E200007: Commit failed异常解决,svncommitfailed 首先2分钟前 ...
- mysql内存使用以及优化中需要的几点注意
1.从内存中读取数据是微秒级别的.而从磁盘读则是毫秒级别的.二者相差一个数量级.所以想优化数据库,第一个要做到的就是优化io. 2.key_buffer_size[global]设置的内存区域大小缓存 ...
- .net调用java写的WebServise时方法总是返回空的问题
解决方法如下: 直接用wsdl.exe根据描述文件生成代理类来调,可避免这样的问题. 同时附上,wsdl工具在.net菜单中的配置方法,参考园友: http://www.cnblogs.com/qfb ...