LeetCode4: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
解题思路:
这题相当于两个大数相加,只不过这里采用的链表的形式,而不是字符串。
解题时最需注意的是,最后一个节点要考虑会不会进位,over =1时,需要增加一个节点。
实现代码:
#include <iostream>
using namespace std; /**
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 */ 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 && l2 == NULL)
return NULL;
ListNode *l3 = new ListNode(-1);
ListNode *tnode = l3;
int over = 0;
while(l1 && l2)
{
int sum = l1->val + l2->val + over;
ListNode *node = new ListNode(sum % 10);
over = sum / 10;
tnode->next = node;
tnode = tnode->next;
l1 = l1->next;
l2 = l2->next;
}
if(l1 == NULL && l2 == NULL && over)//后一个节点,要考虑有没进位
{
ListNode *node = new ListNode(over);
tnode->next = node;
return l3->next;
} ListNode *left = l1;
if(l2)
left = l2;
while(left)
{
int sum = left->val + over;
ListNode *node = new ListNode(sum % 10);
over = sum / 10;
tnode->next = node;
tnode = tnode->next;
left = left->next; }
if(over)//同样,最后一个节点,要考虑有没进位
{
ListNode *node = new ListNode(over);
tnode->next = node;
}
return l3->next; } };
int main(void)
{
return 0;
}
LeetCode4:Add Two Numbers的更多相关文章
- LeetCode-2: Add Two Numbers
[Problem:2-Add Two Numbers] You are given two non-empty linked lists representing two non-negative i ...
- Q2:Add Two Numbers
2. Add Two Numbers 官方的链接:2. Add Two Numbers Description : You are given two non-empty linked lists r ...
- No.002:Add Two Numbers
问题: You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- leetcode:Add Two Numbers
题目描述:You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- LeetCode之“链表”:Add Two Numbers
题目链接 题目要求: You are given two linked lists representing two non-negative numbers. The digits are stor ...
- LeetCode第[2]题(Java):Add Two Numbers (链表相加)——Medium
题目难度:Medium 题目: You are given two non-empty linked lists representing two non-negative integers. The ...
- LeetCode OJ:Add Two Numbers (相加链表之数)
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 面试题: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 ...
随机推荐
- 上学时的HTML+JS+CSS(小总结)
html:超文本标记语言 基本标签: { 文本标签:<pre></pre>:原封不动的保留空白区域. <br />:换行. <hr wid ...
- ssh/openssh
http://www.cnblogs.com/wwufengg/articles/ssh-openssh-detail.html http://www.cnblogs.com/jjkv3/archiv ...
- 每天一个linux命令(61):wget命令
Linux系统中的wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,我们经常要下载一些软件或从远程服务器恢复备份到本地服务器.wget支持HTTP,HTTPS和FTP ...
- Redis Geo: Redis新增位置查询功能
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/144.html 移动互联网增进了人与人之间的联系,其中基于位置信息的服务( ...
- 快速入门系列--WebAPI--04在老版本MVC4下的调整
WebAPI是建立在MVC和WCF的基础上的,原来微软老是喜欢封装的很多,这次终于愿意将http编程模型的相关细节暴露给我们了.在之前的介绍中,基本上都基于.NET 4.5之后版本,其System.N ...
- RESTORE DATABASE的standby选项
RESTORE DATABASE [db1] FROM DISK = N'E:\Backup\db2.bak' , MOVE N'db1_Data' TO N'D:\Data\db2.MDF', MO ...
- centos-lynx
1.官网 http://lynx.isc.org 2.稳定版本 http://invisible-mirror.net/archives/lynx/tarballs/lynx2.8.8rel.2.ta ...
- .NET面试题解析(05)-常量、字段、属性、特性与委托
系列文章目录地址: .NET面试题解析(00)-开篇来谈谈面试 & 系列文章索引 弱小和无知不是生存的障碍,傲慢才是!——<三体> 常见面试题目: 1. const和reado ...
- JSP网站开发基础总结《五》
开始本篇总结之前,首先聊一聊上一篇中存在的一点小问题,上上篇总结数据库创建表时,存在一个问题,name.year.form好像属于关键字,不能做为表的属性,所以大家注意一下,在创建表时保证表的属性不存 ...
- 基于Metronic的Bootstrap开发框架经验总结(4)--Bootstrap图标的提取和利用
在前面的一篇随笔<基于Metronic的Bootstrap开发框架经验总结(1)-框架总览及菜单模块的处理>介绍了菜单模块的处理,主要介绍如何动态从数据库里面获取记录并构建菜单列表.其中菜 ...