-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path117-LinkedList.js
179 lines (150 loc) · 4.83 KB
/
117-LinkedList.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
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
/*
Dada una lista enlazada y un numero N, devolver el nodo N empezando por el final y desde cero.
* Input: 1>2>4>6 2
* Output: 4 (1,2,4)
*/
// ----- IMPLEMENTACION LINKEDLISTS -------
class Node {
constructor (data, nextNode) {
this.data = data;
this.next = nextNode;
}
}
class LinkedList {
constructor() {
this.head = null;
this.size = 0;
}
// agregar info al final de la lista
add(data) {
const newNode = new Node(data, null);
if (!this.head) {
// Si head esta vacio, asignamos al nodo como cabeza
this.head = newNode;
}
else {
// al momento de agregar un nuevo nodo, se debe iterar en toda la lista
// hasta encontrar un nodo cuya referencia al siguiente sea nula
let current = this.head;
while (current.next) {
// iteramos al siguiente elemento y a su next
current = current.next;
}
// al encontrar un current sin un nodo siguiente, lo asignamos
current.next = newNode;
}
// aumentamos el tamaño de la lista
this.size++;
}
// Ingresar info a la lista en un indice especifico
insertAt(data, index) {
// revisamos que el indice sea valido
if (index < 0 || index > this.size) return null;
const newNode = new Node(data);
let current = this.head;
// creamos una variable anterior
let previous;
// Si se quiere agregar el nuevo nodo a la cabeza
if (index === 0) {
// apuntamos el head del nuevo nodo al viejo nodo en head
newNode.next = current;
this.head = newNode;
}
else {
// iteramos hasta el indice
for (let i = 0; i < index; i++) {
// guardamos el nodo anterior
previous = current;
// iteramos al siguiente elemento
current = current.next;
}
// al encontrar el indice, apuntamos el nodo anterior al nuevo nodo
previous.next = newNode;
// apuntamos el nuevo nodo al nodo siguiente del actual
newNode.next = current;
}
}
// imprime la lista enlazada
print() {
// si esta vacia devolvemos vacio
if (this.size === 0) return 'Empty list';
let current = this.head;
let result = ''
while (current) {
// recorremos
result += `${current.data} => `;
current = current.next;
}
result += "X";
return result;
}
removeData(dataToRemove) {
let current = this.head;
let previous = null;
//siempre y cuando current no sea nulo
while (current) {
if (current.data == dataToRemove) {
// si no tiene valor anterior, es el head
if (!previous){
this.head = current.next;
}
else {
// el sig valor del anterior será igual al sig valor del actual
previous.next = current.next;
}
this.size--;
return current.data; // devuelve lo eliminado
}
// si no lo encontramos
previous = current;
current = current.next;
}
return null;
}
// eliminar basado en el indice y no en su valor
removeFrom (index) {
if (index < 0 || index > this.size) return null;
let current = this.head;
let previous = null;
// si se quiere eliminar el primer valor de la lista (head)
if (index === 0) {
// asignamos una nueva head
this.head = current.next;
}
else {
for (let i = 0; i < index; i++) {
previous = current;
current = current.next;
}
previous.next = current.next;
}
this.size--; // la lista disminuira su tamaño
return current.data; // devuelve lo eliminado
}
// la lista esta vacia?
isEmpty () {
if (this.size === 0) return true;
return false;
}
// obtener tamaño
getSize () {
return this.size;
}
}
// ----- IMPLEMENTACION LINKEDLISTS -------
const linkedList = new LinkedList();
linkedList.add(1);
linkedList.add(2);
linkedList.add(4);
linkedList.add(6);
const getNodeByIndex = (list, number) => {
// Si el numero dado es mayor al que podramos contar de nuestro linkedlist, no hacer nada
if (list.getSize() - 1 < number) return;
list = list.head;
// Recorrer el linkedlist hasta llegar al numero de los parametros
for (let i = 0; i < number; i++) {
list = list.next;
}
return list.data;
}
console.log(getNodeByIndex(linkedList, 3)); // 6