-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathundertest.cpp
68 lines (60 loc) · 1.23 KB
/
undertest.cpp
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
/*
Please note that it's Function problem i.e.
you need to write your solution in the form Function(s) only.
Driver Code to call/invoke your function would be added by GfG's Online Judge.*/
// 5 4 4 5
//5 4 3 3 4 5
/*struct Node
{
int data;
Node* next;
}; */
//write a function returns the resultant linked list
Node* addTwoLists(Node* first, Node* second){
// Code here
int count1=1,count2=1;
int number1=0,number2=0;
Node *temp=first, *temp1=second;
//number1
temp=first;
while(temp!=NULL)
{
number1 +=(temp->data)*count1;
temp=temp->next;
count1*=10;
}
// cout<<"n1 "<<number1;
//number2
temp1=second;
while(temp1!=NULL)
{
number2 +=(temp1->data)*count2;
temp1=temp1->next;
count2*=10;
}
// cout<<"n2 "<<number2;
int sum= number1+number2; // 3 9 0
//cout<<"sum "<<sum;
Node *temp2= (Node*) malloc(sizeof(Node));
Node *end=NULL,*head;
while(sum>0)
{
int r= sum%10;
sum/=10;
temp2->data= r;
if(end==NULL)
{
temp2->next=NULL;
end=temp2;
head=temp2;
}
else
{
temp2->data=r;
temp2->next=NULL;
end->next=temp2;
temp2= end;
}
}
return head;
}