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
反向存储,从左往右加 [抄题]: 你有两个用链表代表的整数,其中每个节点包含一个数字.数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头.写出一个函数将两个整数相加,用链表形式返回和.给 ...
随机推荐
- 2019最新Android面试题
原文链接:https://blog.csdn.net/wen_haha/article/details/88362469版权声明:本文为博主原创文章,转载请附上博文链接! 前言 金三银四到来了,找工作 ...
- 简单工厂模式及php实现
简单工厂模式(Simple Factory Pattern): 又称为静态工厂方法(Static Factory Method)模式,它属于类创建型模式.在简单工厂模式中,可以根据参数的不同返回不同类 ...
- LN : leetcode 123 Best Time to Buy and Sell Stock III
lc 123 Best Time to Buy and Sell Stock III 123 Best Time to Buy and Sell Stock III Say you have an a ...
- iOS循环引用
iOS循环引用 当前类的闭包/Block属性,用到了当前类,就会造成循环引用 此闭包/Block应该是当前类的属性,我们经常对Block进行copy,copy到堆中,以便后用. 单方向引用是不会产生循 ...
- 【PostgreSQL-9.6.3】进程及体系结构
本文主要讲述了PG的几个主要进程,以及PG的核心架构.进程和体系结构详见下图: 从上面的体系结构图可以看出来,PG使用经典的C/S架构,进程架构.在服务器端有主进程.服务进程.子进程.共享内存以及文件 ...
- JavaScript——XMLHttpRequest 家族
https://www.zhangxinxu.com/wordpress/2013/10/understand-domstring-document-formdata-blob-file-arrayb ...
- 【译】x86程序员手册37-第10章 初始化
Chapter 10 Initialization 第10章 初始化 After a signal on the RESET pin, certain registers of the 80386 a ...
- C/C++ 运算符重载、数据类型转换
1.运算符就是“+”.“>>”等符号,对运算符重载实质就是对函数的重载,这样运算符就能在原有基础上增加新功能,不能自己定义新运算符,只能对已有运算符重载,重载运算符后不能改变运算符本身的特 ...
- Win7 与win10绘制桌面壁纸的区别
win7使用csrss.exe绘制壁纸. win10使用explorer.exe绘制壁纸.
- vue之packages.json添加注释的正确写法
(1)问题描述 使用vue脚手架vue-cli搭建好项目架构后,在packages.json文件里,加入注释(如下所示).接下来,运行npm run dev命令后出现报错 (2)问题解析 ①记得jso ...