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 ...
随机推荐
- C#中的Delegate
谈C#中的Delegate http://www.cnblogs.com/hyddd/archive/2009/07/26/1531538.html
- 03 - Oracle文件概述
构成Oracle数据库的8种文件类型. 可以把这些文件分成以下几类. Instance相关 参数文件 parameter, initOra file, spfile 跟踪文件 trace file 警 ...
- lintcode:装最多水的容器
装最多水的容器 给定 n 个非负整数 a1, a2, ..., an, 每个数代表了坐标中的一个点 (i, ai).画 n 条垂直线,使得 i 垂直线的两个端点分别为(i, ai)和(i, 0).找到 ...
- unity HideInInspector 默认值 坑 记录 bug
1: 如果 一个public字段 刚开始有默认值,然后你你觉得这个值不应该给别人看设置为HideInInspector 后,你再在代码里面调整这个默认属性的值,这个时候代码里面调整的值无效. ...
- 使用XShell工具密钥认证登录Linux系统
如果你是一名Linux运维,那么Linux服务器的系统安全问题,可能是你要考虑的,而系统登录方式有两种,密码和密钥.哪一种更加安全呢? 无疑是后者! 这里我为大家分享用Xshell利器使用密钥的方式登 ...
- TestDirector安装配置
一.介绍 TestDirector是全球最大的软件测试工具提供商,公司生产企业级测试管理工具,也是业界第一个基于Web的测试管理系统,它可以在公司内部或外部进行全球范围内测试的管理.通过在一个整体的应 ...
- android 使用静态变量传递数据
使用静态变量传递数据之通用方式. 测试应用:当前页面点击button传递数据到一个新的页面显示在textview中. 首先在,mainActivity.xml文件中加入一个button按钮 <B ...
- java异常——RuntimeException和User Define Exception
1.RuntimeException public class RuntimeException { public static void main(String[] args) { // TODO ...
- JQUERY与JS的区别
JQUERY与JS的区别 <style type="text/css"> #aa { width:200px; height:200px; } </style&g ...
- JSON对象末尾多余逗号问题
平时开发用的IE10,没发现这个问题,测试人员对系统兼容性测试时发现了在IE7下存在问题. 问题代码如下: var person = { name: "John", age: 25 ...