-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGFG_Linked_List_Add2_Numbers_LL.cpp
More file actions
96 lines (88 loc) · 2.25 KB
/
GFG_Linked_List_Add2_Numbers_LL.cpp
File metadata and controls
96 lines (88 loc) · 2.25 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
struct node{
ll data;
struct node *next;
};
struct node* insert_head(struct node* head, ll value)
{
struct node* new_node=(struct node*)(malloc(sizeof(struct node)));
new_node->data=value;
new_node->next=head;
head=new_node;
return head;
}
struct node* insert_tail(struct node* head,ll value)
{
struct node* new_node=(struct node*)(malloc(sizeof(struct node)));
new_node->data=value;
new_node->next=NULL;
if(head==NULL)
{
head=new_node;
return head;
}
struct node* temp_point=head;
while(temp_point->next!=NULL)
temp_point=temp_point->next;
temp_point->next=new_node;
return head;
}
void display(struct node* head)
{
struct node* temp_head=head;
cout<<"Linked List is "<<endl;
while(temp_head!=NULL)
{
cout<<temp_head->data<<" ";
temp_head=temp_head->next;
}
cout<<endl;
}
struct node* Add_Numbers_LL(struct node* t1,struct node* t2)
{
struct node *head1=t1, *head2=t2;
ll carry=0;
struct node* head3=NULL;
while(head1!=NULL && head2!=NULL)
{
head3=insert_tail(head3,((head1->data+head2->data+carry)%10));
carry=(head1->data+head2->data+carry)/10;
//cout<<"Carry "<<carry<<endl;
head1=head1->next;
head2=head2->next;
}
while(head1!=NULL)
{
//cout<<"head1"<<head1->data<<" carr"<<carry<<endl;
head3=insert_tail(head3,(head1->data+carry)%10);
carry=(head1->data+carry)/10;
head1=head1->next;
}
while(head2!=NULL)
{
head3=insert_tail(head3,((head2->data)+carry)%10);
carry=(head2->data+carry)/10;
head2=head2->next;
}
return head3;
}
int main() {
struct node *head=NULL;
struct node *head2=NULL;
head=insert_head(head,6);
head=insert_head(head,4);
head=insert_head(head,9);
head=insert_head(head,5);
head=insert_head(head,7);
//head2=insert_head(head2,2);
head2=insert_head(head2,4);
head2=insert_head(head2,8);
//head2=insert_head(head2,20);
display(head);
display(head2);
struct node* head3=Add_Numbers_LL(head,head2);
display(head3);
}