-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathw-linkedList-1.js
52 lines (42 loc) · 1.11 KB
/
w-linkedList-1.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
class Node {
constructor(val) {
this.val = val;
this.next = null;
}
}
const a = new Node("A");
const b = new Node("B");
const c = new Node("C");
const d = new Node("D");
const e = new Node("E");
a.next = b;
b.next = c;
c.next = d;
d.next = e;
// const travelThruNode = (head) => {
// if (head.next === null) return head.val;
// // console.log(travelThruNode(head.next));
// // return head.val + travelThruNode(head.next);
// return travelThruNode(head.next);
// };
// // console.log(travelThruNode(a));
// const printLinkedList = (head) => {
// let current = head;
// while (current) {
// console.log(current.val);
// current = current.next;
// }
// };
// printLinkedList(a);
const travelThru = (head) => {
// if (!head.next) return head.val;
if (!head) return;
// return head.val + travelThruNode(head.next);
console.log(head.val);
// head.next = travelThru(head.next);
// console.log(travelThru(head.next));
// travelThru(head.next);
return travelThru(head.next);
// return head.val;
};
travelThru(a);