File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
Expand file tree Collapse file tree 1 file changed +44
-0
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+ // 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 ) ) ;
You can’t perform that action at this time.
0 commit comments