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
反向存储,从左往右加 [抄题]: 你有两个用链表代表的整数,其中每个节点包含一个数字.数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头.写出一个函数将两个整数相加,用链表形式返回和.给 ...
随机推荐
- kafaka
http://www.360doc.com/content/15/0429/12/9350055_466788393.shtml 一.Kafka中的核心概念 Producer: 特指消息的生产者 Co ...
- absolute元素水平居中
原始(未居中): .con{ width:200px; height:200px; background:#ccc; position:relative; } .abs{ width:40px; he ...
- android环境搭建环境 cordova run android gradle wrapper报错
cordova run android命令报错 Error: Could not find an installed version of Gradle either in Android Studi ...
- 升级 Cocoapods 到1.2.0指定版本,降低版本及卸载
=====================升级版本=================== CocoaPods 1.1.0+ is required to build SnapKit 3.0.0+. 在 ...
- colormap画出的图不是彩色问题
针对matlab2017渲染出的彩色图是黑白的问题. t=labels; t(tstSet(:,end-))=Relabels; t=reshape(t,,); t=t'; figure imshow ...
- Fragment中获取Activity的Context (转)
Fragment中获取Activity的Context时只需要this.getActivity()即可. 而不是许多人说的this.getActivity().getApplicationCo ...
- jquery 微信端 点击物理返回按钮,弹出提示框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- zabbix监控tcp连接并发数
第一步,想在zabbix的web监控tcp连接数,那么要看zabbix-server的版本和zabbix-agent版本是否一致,不然TCP-status图没有数据 也会报错.下图就是版本不符合报错的 ...
- 认识单文件组件.vue 文件
vuejs 自定义了一种.vue文件,可以把html, css, js 写到一个文件中,从而实现了对一个组件的封装, 一个.vue 文件就是一个单独的组件.由于.vue文件是自定义的,浏览器不认识,所 ...
- python selenium等待特定网页元素加载完毕
selenium等待特定元素加载完毕 is_disappeared = WebDriverWait(driver, 8, 0.5, ignored_exceptions=TimeoutExceptio ...