LeetCode——Add Two Numbers
Question:
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
Solution:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* NodeList *next;
* NodeList(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *addTwoNumbers(ListNode *head1, ListNode *head2) {
int n1=,n2=;
ListNode *ptr1=head1,*ptr2=head2;
while(ptr1!=NULL)
{
n1++;
ptr1=ptr1->next;
}
while(ptr2!=NULL)
{
n2++;
ptr2=ptr2->next;
}
vector<int> sum;
int temp;
ListNode *head_long,*head_short;
if(n1>=n2)
{
head_long=head1;
head_short=head2;
}
else
{
head_long=head2;
head_short=head1;
}
/////
ListNode *pt1=head_long,*pt2=head_short;
while(pt2!=NULL)
{
temp=pt1->val+pt2->val;
if(temp>=)
{
sum.push_back(temp%);
if(pt1->next==NULL)
{
sum.push_back();
}
else
pt1->next->val+=;
}
else
sum.push_back(temp);
pt1=pt1->next;pt2=pt2->next;
}
while(pt1!=NULL)
{
temp=pt1->val;
if(temp>=)
{
sum.push_back(temp%);
if(pt1->next==NULL)
{
sum.push_back();
}
else
pt1->next->val+=;
}
else
sum.push_back(temp);
pt1=pt1->next;
}
ListNode *result=new ListNode();
ListNode *rp=new ListNode();
rp=result;
ListNode *rq; for(vector<int>::iterator iter=sum.begin();iter!=sum.end();iter++)
{
rq=new ListNode();
rq->val=*iter;
rp->next=rq;
rp=rq;
} rp->next=NULL;
result=result->next;
return result;
}
};

LeetCode——Add Two Numbers的更多相关文章
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- LeetCode: Add Two Numbers 解题报告
Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...
- Leetcode:Add Two Numbers分析和实现
Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...
- [LeetCode] Add Two Numbers题解
Add Two Numbers: You are given two non-empty linked lists representing two non-negative integers. Th ...
- [Leetcode] Add two numbers 两数之和
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- [LeetCode] Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- [LeetCode] Add Two Numbers 链表
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
随机推荐
- Spring mvc json null
http://blog.csdn.net/zdsdiablo/article/details/9429263
- 从.NET 1.1 升级到.NET 4.0 遇到 线程间操作无效: 从不是创建控件 [XX] 的线程访问它.
有两种方式解决 1.在窗体构造函数中写Control.CheckForIllegalCrossThreadCalls =false;2.使用Invoke等委托函数 问题原因是 .NET2.0 以后拒绝 ...
- zoj 3513 Human or Pig 博弈论
思路:P态的所有后继全为H态,第一个格子为P态,第一行和第一列为H态. 代码如下: #include<iostream> #include<cstdio> #include&l ...
- (转)Android之ListView原理学习与优化总结
转自: http://jishu.zol.com.cn/12893.html 在整理前几篇文章的时候有朋友提出写一下ListView的性能优化方面的东西,这个问题也是小马在面试过程中被别人问到的….. ...
- BZOJ 2661: [BeiJing wc2012]连连看 费用流
2661: [BeiJing wc2012]连连看 Description 凡是考智商的题里面总会有这么一种消除游戏.不过现在面对的这关连连看可不是QQ游戏里那种考眼力的游戏.我们的规则是,给出一个闭 ...
- android模拟器(genymotion)+appium+python 框架执行基本原理(目前公司自己写的)
android模拟器(genymotion)+appium+python 框架执行的基本过程: 1.Push.initDate(openid)方法 //业务数据初始化 1.1 v5db.p ...
- Struts2笔记——自定义拦截器
需要两个步骤,自定义类实现拦截器接口和在struts.xml注册拦截器 =============================== 1.自定义类实现com.opensymphony.xwork2. ...
- *在Win7中安装JDK1.7并配置环境变量
安装的过程就不废话了. 下面是环境变量的配置. 1. 配置环境变量 单机右键‘计算机--属性’ 2.点击高级系统设置 3.点击‘环境变量’ 4.增加"JAVA_HOME"系统变 ...
- 《MySQL悲观锁总结和实践》乐观锁
mysql乐观锁总结和实践 博客分类: MyBatis 数据库 mysql数据库乐观锁悲观锁 上一篇文章<MySQL悲观锁总结和实践>谈到了MySQL悲观锁,但是悲观锁并不是适用于任何场景 ...
- highCharts图表入门实例
本文通过讲解Highcharts生成一个简单的3D柱状图实例来学习Highcharts的使用. JSP 页面 1.需要引入的js文件 <script src="<%=basePa ...