-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadditionlinkedlist.cpp
More file actions
73 lines (63 loc) · 1.12 KB
/
Copy pathadditionlinkedlist.cpp
File metadata and controls
73 lines (63 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<iostream>
using namespace std;
struct node{
int data;
node *next;
};
struct node * newnode(int d){
node * temp=new node;
temp->data=d;
temp->next=NULL;
return temp;
}
int helpsm(node *head1,node *head2,node *head3){
if(head1->next==NULL && head2->next==NULL){
int t=head1->data+head2->data;
head3->data=t%10;
return t/10;
}
else{
int t=head1->data+head2->data;
head3->data=(helpsm(head1->next,head2->next,head3->next)+t)%10;
return t/10;
}
}
void sm(node * head1,node * head2){
int l1=0,l2=0;
node *t1=head1;node *t2=head2;
while(t1!=NULL)
{
l1++;
t1=t1->next;
}
while(t2!=NULL)
{
l2++;
t2=t2->next;
}
if(l1 ==l2){
struct node *head3=newnode(0);
node *t33=head3;
while(l1>0){
t33->next=newnode(0);
t33=t33->next;
}
int l= helpsm(head1,head2,head3);
node *t3=head3;
t3->data+=l;
while(t3!=NULL){
cout<<t3->data<<" ";
t3=t3->next;
}
}
}
int main(){
struct node *head1=newnode(1);
head1->next=newnode(4);
head1->next->next=newnode(5);
struct node *head2=newnode(2);
head2->next=newnode(4);
head2->next->next=newnode(7);
sm(head1,head2);
return 0;
}