-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0002_Add_Two_Numbers.js
65 lines (61 loc) · 2.52 KB
/
0002_Add_Two_Numbers.js
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
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var addTwoNumbers = function(l1, l2) {
let summedList = new ListNode(0, null); // create new linked list to return the sum of two input linked-lists
let head = summedList; // copy linked list but same reference
let sum = 0;
let carry = 0;
while(l1 !== null || l2 !== null || sum > 0) { // while input linked-lists are not at the tails or sum is bigger than 0
if(l1 !== null) { // if input linked list l1 is not at the tail
sum += l1.val; // add to sum
l1 = l1.next; // and go to next node
}
if(l2 !== null) { // if input linked list l2 is not at the tail
sum += l2.val; // add to sum
l2 = l2.next; // and go to next node
}
if(sum >= 10) { // if sum is bigger than 10
carry = 1; // create carry
sum -= 10; // and leave last digit only
}
head.next = new ListNode(sum, null); // create new node with last digit and connect to head
head = head.next; // to repeat this process, head should be head.next
// otherwise it will overwrite second node
sum = carry;
carry = 0;
}
return summedList.next; // return next node of summedList because it starts with 0
};
///////////////////////////////////////////////////////////////////////////
///////////////////////// Test Input Linked-lists /////////////////////////
///////////////////////////////////////////////////////////////////////////
const linkedList1 = {
val: 2,
next: {
val: 4,
next: {
val: 3,
next: null
}
}
};
const linkedList2 = {
val: 5,
next: {
val: 6,
next: {
val: 4,
next: null
}
}
};