-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathadd2linkedlist.cpp
166 lines (137 loc) · 3.18 KB
/
add2linkedlist.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(int d){
this->data = d;
this->next = NULL;
}
};
void insertAtEnd(Node* &head, Node* &end, int d){
if(head == NULL){
Node* node1 = new Node(d);
head = node1;
end = node1;
}
else{
Node* node1 = new Node(d);
end->next = node1;
end = node1;
}
}
Node* reverse(Node* head){
if(head == NULL){
return NULL;
}
Node* forw = NULL;
Node* prev = NULL;
Node* curr = head;
while(curr!=NULL){
forw = curr->next;
curr->next = prev;
prev = curr;
curr = forw;
}
return prev;
}
void insertAtTail(Node* &head, Node* &tail, int d){
if(head == NULL){
Node* node1 = new Node(d);
head = node1;
tail = node1;
}
else{
Node* node1 = new Node(d);
tail->next = node1;
tail = node1;
}
}
void insertAtHead(Node* &head, int d){
//create a new node
Node *temp = new Node(d);
temp->next = head;
head = temp;
}
Node* add(Node* first,Node* second){
int carry = 0;
Node* ansHead = NULL;
Node* ansTail = NULL;
while(first!=NULL || second!=NULL ||carry!=0){
int val1 = 0;
if(first!=NULL){
val1 = first->data;
}
int val2 = 0;
if(second!=NULL){
val2 = second->data;
}
int sum = carry+val1+val2;
int digit = sum%10;
//create node and push ans in ans linked list
insertAtTail(ansHead,ansTail, digit);
carry=sum/10;
if(first!=NULL){
first = first->next;
}
if(second!=NULL){
second = second->next;
}
}
return ansHead;
}
Node* sum(Node* first, Node* second){
//Step 1 to reverse both list
first = reverse(first);
second = reverse(second);
//Step2 add from left
Node* ans = add(first,second);
//Step3 reverse ans list
ans = reverse(ans);
return ans;
}
void insertatpos(Node* &head,int data,int pos,Node* &tail){
if(pos==1){
insertathead(head,data);
return ;
}
Node* temp=new Node(data);
Node*pre=head;
for(int i=1;i<pos-1;i++){
pre=pre->next;
}
if(pre->next==NULL){
insertattail(tail,data);
return ;
}
temp->next=pre->next;
pre->next->prev=temp;
pre->next=temp;
temp->prev=pre;
}
void print(Node* head){
Node* temp = head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
int main(){
Node* head = NULL;
Node* end = NULL;
insertAtEnd(head,end,3);
insertAtEnd(head,end,5);
print(head);
Node* head1 = NULL;
Node* end1 = NULL;
insertAtEnd(head1,end1,4);
insertAtEnd(head1,end1,7);
insertAtEnd(head1,end1,8);
print(head1);
insertatpos(head1,80,2,end1);
Node* result = sum(head,head1);
print(result);
return 0;
}