File tree Expand file tree Collapse file tree 3 files changed +58
-4
lines changed
Expand file tree Collapse file tree 3 files changed +58
-4
lines changed Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 1+
12class 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
89a = new Node ( 'a' )
Original file line number Diff line number Diff line change 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 ) ) ;
You can’t perform that action at this time.
0 commit comments