@@ -2,74 +2,39 @@ import LinkedListNode from './LinkedListNode';
22
33export default class LinkedList {
44 constructor ( ) {
5+ /** @var LinkedListNode */
56 this . head = null ;
7+
8+ /** @var LinkedListNode */
69 this . tail = null ;
710 }
811
9- prepend ( { value, key = null } ) {
10- const newNode = new LinkedListNode ( { value, key, next : this . head } ) ;
11-
12+ prepend ( value ) {
1213 // Make new node to be a head.
13- this . head = newNode ;
14+ this . head = new LinkedListNode ( value , this . head ) ;
1415
15- return newNode ;
16+ return this ;
1617 }
1718
18- append ( { value, key = null } ) {
19- const newNode = new LinkedListNode ( { value, key } ) ;
19+ append ( value ) {
20+ const newNode = new LinkedListNode ( value ) ;
2021
2122 // If there is no head yet let's make new node a head.
2223 if ( ! this . head ) {
2324 this . head = newNode ;
2425 this . tail = newNode ;
2526
26- return newNode ;
27+ return this ;
2728 }
2829
2930 // Attach new node to the end of linked list.
3031 this . tail . next = newNode ;
3132 this . tail = newNode ;
3233
33- return newNode ;
34+ return this ;
3435 }
3536
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 ) {
7338 if ( ! this . head ) {
7439 return null ;
7540 }
@@ -102,37 +67,28 @@ export default class LinkedList {
10267 return deletedNode ;
10368 }
10469
105- deleteByKey ( key ) {
70+ find ( { value = undefined , callback = undefined } ) {
10671 if ( ! this . head ) {
10772 return null ;
10873 }
10974
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-
11875 let currentNode = this . head ;
11976
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 ;
12781 }
128- }
12982
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 ;
13389 }
13490
135- return deletedNode ;
91+ return null ;
13692 }
13793
13894 deleteTail ( ) {
@@ -177,32 +133,15 @@ export default class LinkedList {
177133 return deletedHead ;
178134 }
179135
180- findByKey ( key ) {
181- let currentNode = this . head ;
136+ toString ( callback ) {
137+ const nodeStrings = [ ] ;
182138
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 = [ ] ;
195139 let currentNode = this . head ;
196-
197140 while ( currentNode ) {
198- listArray . push ( currentNode . toString ( ) ) ;
141+ nodeStrings . push ( currentNode . toString ( callback ) ) ;
199142 currentNode = currentNode . next ;
200143 }
201144
202- return listArray ;
203- }
204-
205- toString ( ) {
206- return this . toArray ( ) . toString ( ) ;
145+ return nodeStrings . toString ( ) ;
207146 }
208147}
0 commit comments