Skip to content

Commit 85e72b9

Browse files
committed
se crea metodos de suma para una linkedList
1 parent 84a156f commit 85e72b9

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

linkedList.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+

linkedListPrint.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
12
class Node {
2-
constructor(val){
3-
this.value = val;
4-
this.next = null
5-
}
3+
constructor(val){
4+
this.value = val;
5+
this.next = null
6+
}
67
}
78
//create nodes
89
a = new Node('a')

linkedListSuma.js

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

0 commit comments

Comments
 (0)