-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSum.js
More file actions
134 lines (126 loc) · 2.73 KB
/
Sum.js
File metadata and controls
134 lines (126 loc) · 2.73 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
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
class Node {
constructor(data,next = null){
this.data = data;
this.next = next;
}
}
class LinkedList{
constructor(){
this.head = null;
this.size = null;
}
insertFirst(data){
const node = new Node(data,this.head)
this.head = node;
this.size++
}
getFirst(){
return this.head
}
getLast(){
if (!this.head){
return null;
} else {
let node = this.head;
while(node.next){
node = node.next
}
return node;
}
}
print(){
var node = this.head;
console.log('start of linked list');
while (node !== null) {
console.log(node.data);
node = node.next;
}
console.log('end of linked list');
}
kth(index){
var counter = 0;
var node = this.head;
var cool = this.head
while(node){
counter++
node = node.next;
}
var find = counter - index;
counter = 0;
while(cool){
if (counter === find){
return cool;
}
counter++
cool = cool.next
}
}
sum(){
var arr = [];
var node = this.head;
while(node){
arr.unshift(node.data)
node = node.next
}
var middle = Math.floor(arr.length/2)
var first = arr.slice(0,middle)
var second = arr.slice(middle)
first = first.join('')
first = + first
second = second.join('')
second = + second;
if(arr.length === 3){
sum = arr.join('')
sum = + sum
var woo = sum.toString().split('').reverse('').map(function(number){
return + number
})
var cool = new LinkedList();
for (var j = 0; j < woo.length ; j++){
cool.insertFirst(woo[j])
}
return cool;
}
if (second){
var sum = first + second;
} else {
sum = first;
}
var array = sum.toString().split('').reverse('').map(function(number){
return + number
});
var Linked = new LinkedList();
for (let i = 0; i < array.length ; i ++){
Linked.insertFirst(array[i])
}
return Linked;
}
removeDuplicates(){
let curr = this.head.next
let prev = this.head;
var hash = {};
if (prev){
hash[prev.data] = 1;
}
while (curr){
if (!hash[curr.data]){
hash[curr.data] = 1;
prev = curr;
curr = curr.next;
} else {
var store = curr.next;
prev.next = store;
curr = store;
}
}
return hash;
}
}
var LL = new LinkedList();
LL.insertFirst(8)
LL.insertFirst(7)
LL.insertFirst(6)
LL.insertFirst(6)
LL.insertFirst(5)
LL.insertFirst(4)
console.log(LL.sum())