forked from kloklo90/LeetcodeChallenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddTwoNumbers.js
122 lines (102 loc) · 2.99 KB
/
AddTwoNumbers.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
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
/**
* 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}
*/
function linkedList(arr){
return arr.reduceRight((next, value) => ({value, next}), null);
}
var addTwoNumbers = function(l1, l2) {
var firstLiteral = [2, 4, 3];
var secondLiteral = [5, 6, 4];
var firstNumber = parseInt(firstLiteral.reverse().join('').toString());
var secondNumber = parseInt(secondLiteral.reverse().join('').toString());
var result = firstNumber + secondNumber;
/************* GREAT METHOD */
var resultArray = Array.from(String(result), Number).reverse();
console.log(result);
console.log(resultArray);
// console.log(firstLiteral.join(''));
return linkedList(resultArray);
};
/**
* 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}
*/
class Node {
constructor(data) {
this.data = data
this.next = null
}
}
class ListNode {
constructor(head = null) {
this.head = head
}
}
function insertFirst(data) {
this.head = new Node(data, this.head);
this.size++;
}
function convertListToArray(linkedlist) {
console.log(linkedlist);
//let numberArray = new Array(linkedlist.length);
//let current = linkedlist[0];
linkedlist.forEach((e) => {
console.log(e);
})
//console.log(current);
// while(current) {
// console.log(current.data);
// current = current.next;
// }
}
var addTwoNumbers = function(l1, l2) {
var firstHead = new Node(0);
var secondHead = new Node(0);
firstHead.next = l1;
secondHead.next = l2;
convertListToArray(firstHead.next);
// var firstLiteral = [2, 4, 3];
// var secondLiteral = [5, 6, 4];
// var firstNumber = parseInt(firstLiteral.reverse().join('').toString());
// var secondNumber = parseInt(secondLiteral.reverse().join('').toString());
// var result = firstNumber + secondNumber;
// var resultArray = Array.from(String(result), Number).reverse();
// console.log(result);
// console.log(resultArray);
// // const newArr = resultArray.map(item => new ListNode(item));
// let head = new ListNode(resultArray[0]);
// for (let i = 1; i < resultArray.length; i++) {
// let nextNode = new ListNode(resultArray[i]);
// if (i == 1 ) {
// //First Connect
// head.next = nextNode;
// } else {
// nextNode.next = "happy";
// }
// }
//console.log(newArr);
// let list = new LinkedList(ne)
// console.log(firstLiteral.join(''));
// return linkedList(resultArray);
return l1;
};
// function linkedList(arr){
// return arr.reduceRight((next, value) => ({value, next}), null);
// }