1
+ // --- Directions
2
+ // Implement classes Node and Linked Lists
3
+ // See 'directions' document
4
+
5
+ class Node {
6
+ constructor ( data , next = null ) {
7
+ this . data = data ;
8
+ this . next = next ;
9
+ }
10
+ }
11
+
12
+ class LinkedList {
13
+ constructor ( ) {
14
+ this . head = null ;
15
+ }
16
+
17
+ insertFirst ( data ) {
18
+ // this.head = new Node(data, this.head);
19
+ this . insertAt ( data , 0 ) ;
20
+ }
21
+
22
+ size ( ) {
23
+ let counter = 0 ;
24
+ let node = this . head ;
25
+ while ( node ) {
26
+ counter ++ ;
27
+ node = node . next ;
28
+ }
29
+ return counter ;
30
+ }
31
+
32
+ getFirst ( ) {
33
+ // return this.head;
34
+ return this . getAt ( 0 ) ;
35
+ }
36
+
37
+ getLast ( ) {
38
+ // if(!this.head) {
39
+ // return null;
40
+ // }
41
+
42
+ // let node = this.head;
43
+ // while(node) {
44
+ // if(!node.next) {
45
+ // return node;
46
+ // }
47
+ // node = node.next;
48
+ // }
49
+
50
+ return this . getAt ( this . size ( ) - 1 ) ;
51
+ }
52
+
53
+ clear ( ) {
54
+ this . head = null ;
55
+ }
56
+
57
+ removeFirst ( ) {
58
+ // if(!this.head) {
59
+ // return;
60
+ // }
61
+
62
+ // this.head = this.head.next;
63
+ this . removeAt ( 0 ) ;
64
+ }
65
+
66
+ removeLast ( ) {
67
+
68
+ // if(!this.head) {
69
+ // return;
70
+ // } else if(!this.head.next) {
71
+ // previous = null;
72
+ // return;
73
+ // }
74
+
75
+ // let previous = this.head;
76
+ // let node = this.head.next;
77
+
78
+ // while(node.next) {
79
+ // previous = node;
80
+ // node = node.next;
81
+ // }
82
+
83
+ // previous.next = null;
84
+
85
+ this . removeAt ( this . size ( ) - 1 ) ;
86
+ }
87
+
88
+ insertLast ( data ) {
89
+ // if(!this.head) {
90
+ // this.head = new Node(data);
91
+ // } else {
92
+ // this.getLast().next = new Node(data);
93
+ // }
94
+ this . insertAt ( data , this . size ( ) - 1 ) ;
95
+ }
96
+
97
+ getAt ( index ) {
98
+ let counter = 0 ;
99
+ let node = this . head ;
100
+ while ( node ) {
101
+ if ( counter === index ) {
102
+ return node ;
103
+ }
104
+
105
+ counter ++ ;
106
+ node = node . next ;
107
+ }
108
+
109
+ return null ;
110
+ }
111
+
112
+ removeAt ( index ) {
113
+ if ( ! this . head ) {
114
+ return ;
115
+ }
116
+
117
+ if ( index === 0 ) {
118
+ this . head = this . head . next ;
119
+ return ;
120
+ }
121
+
122
+ const previous = this . getAt ( index - 1 ) ;
123
+ if ( ! previous || ! previous . next ) {
124
+ return ;
125
+ }
126
+ previous . next = previous . next . next ;
127
+ }
128
+
129
+ insertAt ( data , index ) {
130
+ if ( ! this . head ) {
131
+ this . head = new Node ( data ) ;
132
+ return ;
133
+ }
134
+
135
+ if ( index === 0 ) {
136
+ this . head = new Node ( data , this . head ) ;
137
+ return ;
138
+ }
139
+
140
+ const previous = this . getAt ( index - 1 ) || this . getLast ( ) ;
141
+ const node = new Node ( data , previous . next ) ;
142
+ previous . next = node ;
143
+ }
144
+
145
+ forEach ( fn ) {
146
+ let node = this . head ;
147
+ let counter = 0 ;
148
+ while ( node ) {
149
+ fn ( node , counter ) ;
150
+ node = node . next ;
151
+ counter ++ ;
152
+ }
153
+ }
154
+
155
+ * [ Symbol . iterator ] ( ) {
156
+ let node = this . head ;
157
+ while ( node ) {
158
+ yield node ;
159
+ node = node . next ;
160
+ }
161
+ }
162
+ }
163
+
164
+ module . exports = { Node, LinkedList } ;
0 commit comments