-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathadd_two_numbers.js
More file actions
71 lines (65 loc) · 1.88 KB
/
add_two_numbers.js
File metadata and controls
71 lines (65 loc) · 1.88 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
/**
* @author Anirudh Sharma
*/
var addTwoNumbers = function (l1, l2) {
// Head of the new linked list - this is the head of the resultant list
let head = null;
// Reference of head which is null at this point
let temp = null;
// Carry
let carry = 0;
// Loop for the two lists
while (l1 !== null || l2 !== null) {
// At the start of each iteration, we should add carry from the last iteration
let sum = carry;
// Since the lengths of the lists may be unequal, we are checking if the
// current node is null for one of the lists
if (l1 != null) {
sum += l1.val;
l1 = l1.next;
}
if (l2 != null) {
sum += l2.val;
l2 = l2.next;
}
// At this point, we will add the total sum % 10 to the new node
// in the resultant list
let node = new ListNode(Math.floor(sum) % 10);
// Carry to be added in the next iteration
carry = Math.floor(sum / 10);
// If this is the first node or head
if (temp == null) {
temp = head = node;
}
// For any other node
else {
temp.next = node;
temp = temp.next;
}
}
// After the last iteration, we will check if there is carry left
// If it's left then we will create a new node and add it
if (carry > 0) {
temp.next = new ListNode(carry);
}
return head;
};
class ListNode {
constructor(val) {
this.val = val;
this.next = null;
}
}
let head1 = new ListNode(2);
head1.next = new ListNode(4);
head1.next.next = new ListNode(3);
let head2 = new ListNode(5);
head2.next = new ListNode(6);
head2.next.next = new ListNode(4);
let result = addTwoNumbers(head1, head2);
let s = "";
while (result !== null) {
s += result.val + " ";
result = result.next;
}
console.log(s);