-
Notifications
You must be signed in to change notification settings - Fork 83
/
Add two numbers represented by linked lists.java
189 lines (162 loc) · 4.7 KB
/
Add two numbers represented by linked lists.java
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
public class linkedlistATN
{
class node
{
int val;
node next;
public node(int val)
{
this.val = val;
}
}
// Function to print linked list
void printlist(node head)
{
while (head != null)
{
System.out.print(head.val + " ");
head = head.next;
}
}
node head1, head2, result;
int carry;
/* A utility function to push a value to linked list */
void push(int val, int list)
{
node newnode = new node(val);
if (list == 1)
{
newnode.next = head1;
head1 = newnode;
}
else if (list == 2)
{
newnode.next = head2;
head2 = newnode;
}
else
{
newnode.next = result;
result = newnode;
}
}
// Adds two linked lists of same size represented by
// head1 and head2 and returns head of the resultant
// linked list. Carry is propagated while returning
// from the recursion
void addsamesize(node n, node m)
{
// Since the function assumes linked lists are of
// same size, check any of the two head pointers
if (n == null)
return;
// Recursively add remaining nodes and get the carry
addsamesize(n.next, m.next);
// add digits of current nodes and propagated carry
int sum = n.val + m.val + carry;
carry = sum / 10;
sum = sum % 10;
// Push this to result list
push(sum, 3);
}
node cur;
// This function is called after the smaller list is
// added to the bigger lists's sublist of same size.
// Once the right sublist is added, the carry must be
// added to the left side of larger list to get the
// final result.
void propogatecarry(node head1)
{
// If diff. number of nodes are not traversed, add carry
if (head1 != cur)
{
propogatecarry(head1.next);
int sum = carry + head1.val;
carry = sum / 10;
sum %= 10;
// add this node to the front of the result
push(sum, 3);
}
}
int getsize(node head)
{
int count = 0;
while (head != null)
{
count++;
head = head.next;
}
return count;
}
// The main function that adds two linked lists
// represented by head1 and head2. The sum of two
// lists is stored in a list referred by result
void addlists()
{
// first list is empty
if (head1 == null)
{
result = head2;
return;
}
// first list is empty
if (head2 == null)
{
result = head1;
return;
}
int size1 = getsize(head1);
int size2 = getsize(head2);
// Add same size lists
if (size1 == size2)
{
addsamesize(head1, head2);
}
else
{
// First list should always be larger than second list.
// If not, swap pointers
if (size1 < size2)
{
node temp = head1;
head1 = head2;
head2 = temp;
}
int diff = Math.abs(size1 - size2);
// move diff. number of nodes in first list
node temp = head1;
while (diff-- >= 0)
{
cur = temp;
temp = temp.next;
}
// get addition of same size lists
addsamesize(cur, head2);
// get addition of remaining first list and carry
propogatecarry(head1);
}
// if some carry is still there, add a new node to
// the front of the result list. e.g. 999 and 87
if (carry > 0)
push(carry, 3);
}
// Driver program to test above functions
public static void main(String args[])
{
linkedlistATN list = new linkedlistATN();
list.head1 = null;
list.head2 = null;
list.result = null;
list.carry = 0;
int arr1[] = { 9, 9, 9 };
int arr2[] = { 1, 8 };
// Create first list as 9->9->9
for (int i = arr1.length - 1; i >= 0; --i)
list.push(arr1[i], 1);
// Create second list as 1->8
for (int i = arr2.length - 1; i >= 0; --i)
list.push(arr2[i], 2);
list.addlists();
list.printlist(list.result);
}
}