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
代码:
vector<int> v1;
vector<int> v2;
int num1;
int num2;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
void insertAtTail(ListNode* &reslist,int val)
{
ListNode* p=reslist;
while (p->next)
p=p->next;
ListNode* newNode=(ListNode*)malloc(sizeof(ListNode));
newNode->val=val;
newNode->next=p->next;
p->next=newNode; }
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
if(l1==NULL&&l2==NULL) return NULL;
ListNode* reslist=NULL;
int jinwei=;
while(l1!=NULL&&l2!=NULL){
if((l1->val+l2->val+jinwei)<){
if(reslist==NULL){
reslist=(ListNode*)malloc(sizeof(ListNode));
reslist->val=l1->val+l2->val+jinwei;
reslist->next=NULL;
}else{
insertAtTail(reslist,l1->val+l2->val+jinwei);
}
jinwei=;
}else{
if(reslist==NULL){
reslist=(ListNode*)malloc(sizeof(ListNode));
reslist->val=l1->val+l2->val+jinwei-;
reslist->next=NULL;
}else{
insertAtTail(reslist,l1->val+l2->val+jinwei-);
}
jinwei=;
}
l1=l1->next;
l2=l2->next;
}
while(l1!=NULL){
if(l1->val+jinwei<){
insertAtTail(reslist,l1->val+jinwei);
jinwei=;
}else{
insertAtTail(reslist,l1->val+jinwei-);
jinwei=;
}
l1=l1->next;
}
while(l2!=NULL){
if(l2->val+jinwei<){
insertAtTail(reslist,l2->val+jinwei);
jinwei=;
}else{
insertAtTail(reslist,l2->val+jinwei-);
jinwei=;
}
l2=l2->next;
}
if(l1==NULL&&l2==NULL&&jinwei!=){
insertAtTail(reslist,jinwei);
}
return reslist;
}
}; void CreateListHead(ListNode* &head, int n)
{
int j=;
head = (ListNode*)malloc(sizeof(ListNode));
head->next=NULL;
head->val=v1[j++];
ListNode* p=head;
for (int i=;i<n;++i)
{
ListNode* newNode;
newNode = (ListNode*)malloc(sizeof(ListNode));
p->next=newNode;
newNode->next=NULL;
newNode->val=v1[j++];
p=p->next;
}
}
void CreateListHead2(ListNode* &head, int n)
{
int j=;
head = (ListNode*)malloc(sizeof(ListNode));
head->next=NULL;
head->val=v2[j++];
ListNode* p=head;
for (int i=;i<n;++i)
{
ListNode* newNode;
newNode = (ListNode*)malloc(sizeof(ListNode));
p->next=newNode;
newNode->next=NULL;
newNode->val=v2[j++];
p=p->next;
}
}
int main()
{
freopen("C:\\Users\\Administrator\\Desktop\\a.txt","r",stdin);
cin>>num1;
for (int i=;i<num1;++i)
{
int temp;
cin>>temp;
v1.push_back(temp);
}
cin>>num2;
for (int i=;i<num2;++i)
{
int temp;
cin>>temp;
v2.push_back(temp);
}
ListNode* head1=NULL;
CreateListHead(head1,num1);
ListNode* head2=NULL;
CreateListHead2(head2,num2);
Solution so;
ListNode* res=so.addTwoNumbers(head1,head2);
return ;
}
Add Two Numbers(链表)的更多相关文章
- LeetCode-2. Add Two Numbers(链表实现数字相加)
1.题目描述 You are given two non-empty linked lists representing two non-negative integers. The digits a ...
- [LeetCode] Add Two Numbers 链表
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【LeetCode】2.Add Two Numbers 链表数相加
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- 002 Add Two Numbers 链表上的两数相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- leetcode 2 Add Two Numbers(链表)
数字反过来这个没有什么麻烦,就是镜像的去算十进制加法就可以了,然后就是简单的链表. /** * Definition for singly-linked list. * struct ListNode ...
- [LeetCode]2. Add Two Numbers链表相加
注意进位的处理和节点为null的处理 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int flag = 0; ListNode ...
- Add Two Numbers - C++链表操作
题目意思很简单,两个链表分别表示两个数,将两个数相加的结果存入一个新的链表中. 思路同样很简单:两个链表如果一样长,对应位置相加,如果某一个链表多了,则根据加的结果有无进位继续处理,全部结束后要考虑会 ...
- LeetCode 2. add two numbers && 单链表
add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...
- 链表求和12 · Add Two Numbers
反向存储,从左往右加 [抄题]: 你有两个用链表代表的整数,其中每个节点包含一个数字.数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头.写出一个函数将两个整数相加,用链表形式返回和.给 ...
随机推荐
- [BZOJ2190][SDOI2008]仪仗队 数学
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2190 看到这道题首先想到了NOI2010的能量采集,这不就是赤裸裸的弱化版吗?直接上莫比乌 ...
- java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to
在做android解析服务器传来的json时遇到的错误. 服务器传来的数据格式 [{"," id":"7ef6815938394fce88a5873312b66 ...
- greenplum4.3.8.2安装
GREENPLUM总体结构: 数据库由Master Severs和Segment Severs通过Interconnect互联组成. Master主机负责:建立与客户端的连接和管理:SQL的解析并 ...
- Node.js——body方式提交数据
引入核心模块 http,利用其 api(http.createServer) 返回一个 http.server 实例,这个实例是继承于net.Server,net.Server 也是通过net.cre ...
- Swift 性能相关
起初的疑问源自于「在 Swift 中的, Struct:Protocol 比 抽象类 好在哪里?」.但是找来找去都是 Swift 性能相关的东西.整理了点笔记,供大家可以参考一下. 一些疑问 在正题开 ...
- wifi钓鱼之--Pumpkin
无线钓鱼 前言:请准备一块rt3070的外接网卡 Pumpkin是一款无线安全检测工具WiFi-Pumpkin的使用,利用该工具可以伪造接入点完成中间人攻击,同时也支持一些其它的无线渗透功能.旨在 ...
- 1434:【例题2】Best Cow Fences
1434:[例题2]Best Cow Fences 时间限制: 1000 ms 内存限制: 65536 KB提交数: 263 通过数: 146 [题目描述] 给定一个长度为n的 ...
- Sql Server cross apply和outer apply
with result as( select t.str from( ' str union all ' str union all ' str union all ' str union all ' ...
- 快速创建你xmlhttp的方法
function initxmlhttp() { var xmlhttp try { xmlhttp=new ActiveXObject("Msxml2.XM ...
- HDU - 2612 Find a way(BFS搜索)
题目: 链接 思路: 用BFS分别以‘Y’和‘M’的位置为起点进行两次搜索,并把这两次的搜索结果在一个二维数组中保存下来,在对地图遍历遇到‘@’更行最小值. PS: 如果用‘Y’和‘M’点分别去搜每个 ...