@@ -2,74 +2,39 @@ import LinkedListNode from './LinkedListNode';
2
2
3
3
export default class LinkedList {
4
4
constructor ( ) {
5
+ /** @var LinkedListNode */
5
6
this . head = null ;
7
+
8
+ /** @var LinkedListNode */
6
9
this . tail = null ;
7
10
}
8
11
9
- prepend ( { value, key = null } ) {
10
- const newNode = new LinkedListNode ( { value, key, next : this . head } ) ;
11
-
12
+ prepend ( value ) {
12
13
// Make new node to be a head.
13
- this . head = newNode ;
14
+ this . head = new LinkedListNode ( value , this . head ) ;
14
15
15
- return newNode ;
16
+ return this ;
16
17
}
17
18
18
- append ( { value, key = null } ) {
19
- const newNode = new LinkedListNode ( { value, key } ) ;
19
+ append ( value ) {
20
+ const newNode = new LinkedListNode ( value ) ;
20
21
21
22
// If there is no head yet let's make new node a head.
22
23
if ( ! this . head ) {
23
24
this . head = newNode ;
24
25
this . tail = newNode ;
25
26
26
- return newNode ;
27
+ return this ;
27
28
}
28
29
29
30
// Attach new node to the end of linked list.
30
31
this . tail . next = newNode ;
31
32
this . tail = newNode ;
32
33
33
- return newNode ;
34
+ return this ;
34
35
}
35
36
36
- appendUnique ( { value, key = null } ) {
37
- const newNode = new LinkedListNode ( { value, key } ) ;
38
-
39
- // If there is no head yet let's make new node a head.
40
- if ( ! this . head ) {
41
- this . head = newNode ;
42
- this . tail = newNode ;
43
-
44
- return newNode ;
45
- }
46
-
47
- // Rewind to last node.
48
- let currentNode = this . head ;
49
- while ( currentNode . next !== null ) {
50
- // If there is a node with specified key exists then update it instead of adding new one.
51
- if ( key && currentNode . key === key ) {
52
- currentNode . value = value ;
53
- return currentNode ;
54
- }
55
-
56
- currentNode = currentNode . next ;
57
- }
58
-
59
- // If there is a node with specified key exists then update it instead of adding new one.
60
- if ( key && currentNode . key === key ) {
61
- currentNode . value = value ;
62
- return currentNode ;
63
- }
64
-
65
- // Attach new node to the end of linked list.
66
- currentNode . next = newNode ;
67
- this . tail = newNode ;
68
-
69
- return newNode ;
70
- }
71
-
72
- deleteByValue ( value ) {
37
+ delete ( value ) {
73
38
if ( ! this . head ) {
74
39
return null ;
75
40
}
@@ -102,37 +67,28 @@ export default class LinkedList {
102
67
return deletedNode ;
103
68
}
104
69
105
- deleteByKey ( key ) {
70
+ find ( { value = undefined , callback = undefined } ) {
106
71
if ( ! this . head ) {
107
72
return null ;
108
73
}
109
74
110
- let deletedNode = null ;
111
-
112
- // If the head must be deleted then make 2nd node to be a head.
113
- if ( this . head . key === key ) {
114
- deletedNode = this . head ;
115
- this . head = this . head . next ;
116
- }
117
-
118
75
let currentNode = this . head ;
119
76
120
- // If next node must be deleted then make next node to be a next next one.
121
- while ( currentNode . next ) {
122
- if ( currentNode . next . key === key ) {
123
- deletedNode = currentNode . next ;
124
- currentNode . next = currentNode . next . next ;
125
- } else {
126
- currentNode = currentNode . next ;
77
+ while ( currentNode ) {
78
+ // If callback is specified then try to find node by callback.
79
+ if ( callback && callback ( currentNode . value ) ) {
80
+ return currentNode ;
127
81
}
128
- }
129
82
130
- // Check if tail must be deleted.
131
- if ( this . tail . key === key ) {
132
- this . tail = currentNode ;
83
+ // If value is specified then try to compare by value..
84
+ if ( value !== undefined && currentNode . value === value ) {
85
+ return currentNode ;
86
+ }
87
+
88
+ currentNode = currentNode . next ;
133
89
}
134
90
135
- return deletedNode ;
91
+ return null ;
136
92
}
137
93
138
94
deleteTail ( ) {
@@ -177,32 +133,15 @@ export default class LinkedList {
177
133
return deletedHead ;
178
134
}
179
135
180
- findByKey ( key ) {
181
- let currentNode = this . head ;
136
+ toString ( callback ) {
137
+ const nodeStrings = [ ] ;
182
138
183
- while ( currentNode ) {
184
- if ( currentNode . key === key ) {
185
- return currentNode ;
186
- }
187
- currentNode = currentNode . next ;
188
- }
189
-
190
- return null ;
191
- }
192
-
193
- toArray ( ) {
194
- const listArray = [ ] ;
195
139
let currentNode = this . head ;
196
-
197
140
while ( currentNode ) {
198
- listArray . push ( currentNode . toString ( ) ) ;
141
+ nodeStrings . push ( currentNode . toString ( callback ) ) ;
199
142
currentNode = currentNode . next ;
200
143
}
201
144
202
- return listArray ;
203
- }
204
-
205
- toString ( ) {
206
- return this . toArray ( ) . toString ( ) ;
145
+ return nodeStrings . toString ( ) ;
207
146
}
208
147
}
0 commit comments