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 ...
随机推荐
- .htaccess的基本作用及相关语法介绍
.htaccess是一个纯文本文件,它里面存放着Apache服务器配置相关的指令. .htaccess主要的作用有:URL重写.自定义错误页面.MIME类型配置以及访问权限控制等.主要体现在伪静态的应 ...
- Linux网络编程1——小端模式与大端模式
数据存储优先顺序的转换 计算机数据存储有两种字节优先顺序:高位字节优先(称为大端模式)和低位字节优先(称为小端模式).内存的低地址存储数据的低字节,高地址存储数据的高字节的方式叫小端模式.内存的高地址 ...
- 【pku2115-C Looooops】拓展欧几里得-不定方程
http://poj.org/problem?id=2115 题解:一个变量从A开始加到B,每次加C并mod2^k,问加多少次.转化为不定方程:C*x+2^K*Y=B-A //poj2115 #inc ...
- installation failed with message null
http://stackoverflow.com/questions/33315753/installation-failed-with-message-null-genymotion-error I ...
- lintcode:带环链表
带环链表 给定一个链表,判断它是否有环. 解题 定义两个指针p1 p2 p1每次向前走一步 p2每次向前走两步 当p2能赶上p1的时候说明有环 /** * Definition for ListNod ...
- Eclipse中WEB项目自动部署到Tomcat
原因 很长时间没用Eclipse了,近期由于又要用它做个简单的JSP项目,又要重新学习了,虽然熟悉的很快,但记忆总是很模糊,偶尔犯错,以前很少写博客,现在感觉还是很有必要的,编程中每个人对于犯过的错误 ...
- 怎样在java代码中调用执行shell脚本
// 用法:Runtime.getRuntime().exec("命令"); String shpath="/test/test.sh"; //程序路径 Pro ...
- 百度首页html代码
把百度设为主页 关于百度 About Baidu ©2015 Baidu 使用百度前必读 意见反馈 京ICP证030173号
- JavaScript事件冒泡和事件委托
JavaScript事件冒泡和事件委托 付建宇 - 2 条评论 接触JavaScript不久,学的东西也不是特别多.小雨就是习惯把平时学到的东西拿出来分享.一方面加强自己的印象,一方面可以让自己的经验 ...
- 初识CentOS服务命令大全
(1)系统架构 查看内核 # uname -s -r Linux 2.6.32-358.el6.x86_64 查看发布版本 # cat /etc/redhat-release CentOS relea ...