-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathlinked_list_sum_lists.cpp
68 lines (60 loc) · 1.57 KB
/
linked_list_sum_lists.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
// sum Lists : you have two numbers represented by a linked list
// where each node contains a single digit, write a function
// to add two numbers and returns the sum as linked list
// Program Author: Abhisek Kumar Gupta
#include<bits/stdc++.h>
using namespace std;
class Node{
public:
int data;
Node *next;
Node(int d){
data = d;
}
};
void insert_at_head(Node *&head, int data){
Node *new_node = new Node(data);
new_node->next = head;
head = new_node;
}
void print_linked_list(Node *head){
if(head == NULL)
return;
cout << head->data << "->";
print_linked_list(head->next);
}
void make_linked_list(Node *&head){
int data;
cin >> data;
while(data != -1){
insert_at_head(head, data);
cin >> data;
}
}
Node* add_lists(Node *l1, Node *l2, int carry){
if(l1 == NULL && l2 == NULL && carry == 0)
return NULL;
int value = carry;
if(l1 != NULL)
value += l1->data;
if(l2 != NULL)
value += l2->data;
Node *result = new Node(value % 10);
if(l1 != NULL || l2 != NULL)
result->next = add_lists(l1 == NULL ? NULL : l1->next, l2 == NULL ? NULL : l2->next, value >= 10 ? 1 : 0);
else
result->next = NULL;
return result;
}
int main(){
Node *head1 = NULL;
Node *head2 = NULL;
make_linked_list(head1);
make_linked_list(head2);
print_linked_list(head1);
cout << endl;
print_linked_list(head2);
Node *result = add_lists(head1, head2, 0);
cout << endl;
print_linked_list(result);
}