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 ...
随机推荐
- CentOS6.5下安装JDK
之前一直没有完全的总结出一篇关于Linux下安装Java的过程,今天正好就整理下. 下载jdk 如果在官网下载比较慢,那么可以到我的云盘分享上,下载jdk 1.8.0的版本: 下载地址参考链接 解压缩 ...
- Atitit dsl对于数组的处理以及main函数的参数赋值
Atitit dsl对于数组的处理以及main函数的参数赋值 1.1. 词法解析..添加了[] 方括号的解析支持1 1.2. Ast建立.添加了数组参数的支持..使用了递归下降法..getparam ...
- salesforce 零基础学习(三十)工具篇:Debug Log小工具
开发中查看log日志是必不可少的,salesforce自带的效果显示效果不佳,大概显示效果如下所示: chrome商城提供了apex debug log良好的插件,使debug log信息更好显示.假 ...
- js获取当前时间显示在页面上
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- hibernate(七) hibernate中查询方式详解
序言 之前对hibernate中的查询总是搞混淆,不明白里面具体有哪些东西.就是因为缺少总结.在看这篇文章之前,你应该知道的是数据库的一些查询操作,多表查询等,如果不明白,可以先去看一下 MySQL数 ...
- Enerprise Solution Main 启动方法源代码
.NET 系统以Main方法作为应用程序的启动入口点,Enterprise Solution的启动程序源代码如下: [STAThread] static void Main() { string MA ...
- elastic-job
github源码: https://github.com/dangdangdotcom/elastic-job maven中央仓: http://repo1.maven.org/maven2/com/ ...
- 信息加密之非对称加密算法RSA
前面为大家已经总结了,基于密钥交换的DH算法,现在就为大家再介绍一种基于因子分解的RSA算法,这种加密算法有两种实现形式:1.公钥加密,私钥解密:2.私钥加密,公钥解密.下面就为大家分析一下实现代码, ...
- 如何配置Hyper-V的虚拟机通过主机网络上网 (NAT)
前言 最近开始在Windows 8 上面直接使用Hyper-V的技术来建立虚拟环境进行开发和测试,这样免去了再安装额外软件的需要.在实际使用的时候,尤其是配置网络共享的时候,遇到些问题,与其他一些虚拟 ...
- AngularJS的学习--$on、$emit和$broadcast的使用
AngularJS中的作用域有一个非常有层次和嵌套分明的结构.其中它们都有一个主要的$rootScope(也就说对应的Angular应用或者ng-app),然后其他所有的作用域部分都是继承自这个$ro ...