@@ -78,7 +78,7 @@ class CircularDoublyLinkedList {
78
78
*/
79
79
const newNode = new CircularDoublyLinkedListNode ( data ) ;
80
80
81
- //special case: no items in the list yet
81
+ // special case: no items in the list yet
82
82
if ( this [ head ] === null ) {
83
83
84
84
/*
@@ -413,13 +413,8 @@ class CircularDoublyLinkedList {
413
413
*/
414
414
remove ( index ) {
415
415
416
- // special case: no nodes in the list
417
- if ( this [ head ] === null ) {
418
- throw new RangeError ( `Index ${ index } does not exist in the list.` ) ;
419
- }
420
-
421
- // special case: `index` is an invalid value
422
- if ( index < 0 ) {
416
+ // special cases: no nodes in the list or `index` is an invalid value
417
+ if ( ( this [ head ] === null ) || ( index < 0 ) ) {
423
418
throw new RangeError ( `Index ${ index } does not exist in the list.` ) ;
424
419
}
425
420
@@ -456,13 +451,6 @@ class CircularDoublyLinkedList {
456
451
return current . data ;
457
452
}
458
453
459
- /*
460
- * The `previous` variable keeps track of the node just before
461
- * `current` in the loop below. This is necessary because removing
462
- * an node means updating the previous node's `next` pointer.
463
- */
464
- let previous = null ;
465
-
466
454
/*
467
455
* The `i` variable is used to track how deep into the list we've
468
456
* gone. This is important because it's the only way to know when
@@ -478,9 +466,6 @@ class CircularDoublyLinkedList {
478
466
*/
479
467
do {
480
468
481
- // save the value of current
482
- previous = current ;
483
-
484
469
// traverse to the next node
485
470
current = current . next ;
486
471
@@ -496,8 +481,8 @@ class CircularDoublyLinkedList {
496
481
if ( current !== this [ head ] ) {
497
482
498
483
// skip over the node to remove
499
- previous . next = current . next ;
500
- current . next . previous = previous ;
484
+ current . previous . next = current . next ;
485
+ current . next . previous = current . previous ;
501
486
502
487
// return the value that was just removed from the list
503
488
return current . data ;
0 commit comments