Skip to content

Commit 84a156f

Browse files
committed
se crea linkedList se muestra elementos en un array
1 parent e8d0b69 commit 84a156f

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

linkedListPrint.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Node {
2+
constructor(val){
3+
this.value = val;
4+
this.next = null
5+
}
6+
}
7+
//create nodes
8+
a = new Node('a')
9+
b = new Node('b')
10+
c = new Node('c')
11+
d = new Node('d')
12+
//linkedList
13+
a.next = b;
14+
b.next = c;
15+
c.next = d;
16+
// function linkedListValues(head){
17+
18+
// let current = head;
19+
// const values = [];
20+
21+
// while(current!==null){
22+
// values.push(current.value);
23+
// current = current.next;
24+
// }
25+
// return values
26+
27+
28+
// }
29+
// console.log(linkedListValues(a));
30+
31+
//recursive
32+
33+
const linkedListValues = (head) => {
34+
const values = [];
35+
fillValues(head, values);
36+
return values;
37+
};
38+
39+
const fillValues = (head, values) => {
40+
if (head === null) return;
41+
values.push(head.value);
42+
fillValues(head.next, values);
43+
};
44+
console.log(linkedListValues(a));

0 commit comments

Comments
 (0)