1class LinkedList {
2 constructor() {
3 this.head = null;
4 }
5
6 insertHead(value) {
7 const node = { value, next: this.head };
8 this.head = node;
9 }
10
11 traverse() {
12 let cur = this.head;
13 while (cur !== null) {
14 // visit cur.value
15 cur = cur.next;
16 }
17 }
18
19 delete(value) {
20 let prev = null, cur = this.head;
21 while (cur !== null) {
22 if (cur.value === value) {
23 if (prev === null) this.head = cur.next;
24 else prev.next = cur.next;
25 return;
26 }
27 prev = cur;
28 cur = cur.next;
29 }
30 }
31
32 reverse() {
33 let prev = null, cur = this.head;
34 while (cur !== null) {
35 const next = cur.next;
36 cur.next = prev;
37 prev = cur;
38 cur = next;
39 }
40 this.head = prev;
41 }
42}