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 ...
随机推荐
- DevExpress Form那些事儿
1:设置子窗体依附父窗体 首先将父窗体的属性中 IsMdiContainer 设置为 True , 就是将窗体设置为 MDI窗体.子窗体和父窗体都是继承自RibbonForm的. 代码如下 : ...
- C#两种创建快捷方式的方法
C#两种创建快捷方式的方法http://www.cnblogs.com/linmilove/archive/2009/06/10/1500989.html
- WCF分布式开发步步为赢(8):使用数据集(DataSet)、数据表(DataTable)、集合(Collection)传递数据
数据集(DataSet).数据表(DataTable).集合(Collection)概念是.NET FrameWork里提供数据类型,在应用程序编程过程中会经常使用其来作为数据的载体,属于ADO.NE ...
- mysql之select(二)
union 联合 作用: 把2次或多次查询结果合并起来. 要求:两次查询的列数一致.推荐:查询的每一列,相对应得列类型也一样. 可以来自于多张表.多次sql语句取出的列名可以不一致,此时,以第1个sq ...
- 基于Mongodb的轻量级领域驱动框架(序)
混园子也有些年头了,从各个大牛那儿学了很多东西.技术这东西和中国的料理一样,其中技巧和经验,代代相传(这不是舌尖上的中国广告).转身回头一望,几年来自己也积累了一些东西,五花八门涉猎到各种方向,今日开 ...
- spring定时器,5步完成
spring定时器,5步完成,我们开发的时候会用定时执行任务. 用spring框架时,可以直接使用spring定时功能 1.创建任务调度类,里面一个方法,方法名为work 2. spring配置文件, ...
- Python3导入自定义模块的3种方式
前话 最近跟着廖雪峰的教程学到 模块 这一节.关于如何自定义一个模块,如果大家不懂的话还请先看下面这篇博文 ↓ http://www.liaoxuefeng.com/wiki/001431608955 ...
- HTML5之拖拽(兼容IE和非IE)
前世:项目中需要拖动div,然后和某个div进行位置交换,这不是关键,关键是还要保存位置,然后在下次打开的时候按照保存的位置显示.还好本人功力深厚,一下子就想到了用localStorage来保存,事实 ...
- WAS集群服务的关闭与启动
WAS集群服务的关闭与启动 欢迎转载,转载时请务必注明出处(http://blog.csdn.net/huangyanlong),否则作者保留追究版权法律责任. 表述有错误之处,请您留言或邮件(hyl ...
- 311. Sparse Matrix Multiplication
题目: Given two sparse matrices A and B, return the result of AB. You may assume that A's column numbe ...